Area of polygon with coordinates python
Calculating area from lat/lon polygons in Python
Asked 5 years, 3 months ago. Active 5 months ago. Viewed 14k times. You may look at stackoverflow. Active Oldest Votes. It wasn't readily apparent to me how to use sgillies answer, so here is a more verbose version: import pyproj import shapely import shapely. Philipp Schwarz 1 1 gold badge 2 2 silver badges 9 9 bronze badges. The resulting value is not exactly the same as the one obtained in geojson.
You should probably indicate that partial is not a built-in; pyproj will have to be imported and possibly installed, etc. I noticed pyproj. Is this Answer accurate, or should it be updated? I suspect this applies post PROJ 6.
I don't have enough rep to comment, so I'm making a new answer sorry not sorry import pyproj import shapely import shapely. Vince MartinThurn MartinThurn 33 4 4 bronze badges. Mike Honey Mike Honey 1 1 silver badge 8 8 bronze badges.
Unfortunately that is not enough for me. Is there any way to get the coordinates in a Polygon-shapefile straight away? Looks like there is a problem saving the shapefile to disk. This expands on the answer posted by BradHards :. The error message sounds like pyshp is expecting float s where it is not getting them.
If your coordinate list is a set of int s, try casting them to float s:.
Polygon area calculator
Add a comma in the end of each XY pair. Your polygon will be created in a memory layer in QGIS. After that, you can do whatever you want with it, including Save as Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Ask Question. Asked 6 years, 2 months ago. Active 3 years ago.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have a set of points and would like to know if there is a function for the sake of convenience and probably speed that can calculate the area enclosed by a set of points. Maybe there is something from scipy, matplotlib, numpy, shapely, etc.
I won't be encountering any negative values for either the x or y coordinates Implementation of Shoelace formula could be done in Numpy. Assuming these vertices:. If you have a polygon with holes : Calculate the area of the outer ring and subtrack the areas of the inner rings. If you have self-intersecting rings : You have to decompose them into simple sectors.
By analysis of Mahdi's answer, I concluded that the majority of time was spent doing np. By calculating the correctional term, and then slicing the arrays, there is no need to roll or create a new array. This can be mitigated with a simple coordinate shift:.
Area of a polygon with given n ordered vertices
For example, a common geographic reference system is UTM, which might have x,y coordinates of The product of those two values is You can see that this single product is already at the edge of precision it has the same number of decimal places as the inputs.
Adding just a few of these products, let alone thousands, will result in loss of precision. A simple way to mitigate this is to shift the polygon from large positive coordinates to something closer to 0,0for example by subtracting the centroid as in the code above.
This helps in two ways:. The coordinate shift does not alter the total area, it just makes the calculation more numerically stable. There's an error in the code above as it doesn't take absolute values on each iteration. The above code will always return zero. Here's some alternate code. Given the number of sides, n, and the length of each side, s. Learn more. Calculate area of polygon given x,y coordinates Ask Question. Asked 5 years, 9 months ago. Active 1 year, 3 months ago. Viewed 55k times.
EDIT: points will most likely not be in any specified order clockwise or counterclockwise and may be quite complex as they are a set of utm coordinates from a shapefile under a set of boundaries.Given ordered coordinates of a polygon with n vertices. Find area of the polygon. Here ordered mean that the coordinates are given either in clockwise manner or anticlockwise from first vertex to last.
We can compute area of a polygon using Shoelace formula. Below is implementation of above formula. Why is it called Shoelace Formula?
The formula is called so because of the way we evaluate it. How does this work? We can always divide a polygon into triangles. The area formula is derived by taking each edge AB, and calculating the signed area of triangle ABO with a vertex at the origin O, by taking the cross-product which gives the area of a parallelogram and dividing by 2. As one wraps around the polygon, these triangles with positive and negative area will overlap, and the areas between the origin and the polygon will be cancelled out and sum to 0, while only the area inside the reference triangle remains.
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Writing code in comment? Please use ide. X[i], Y[i] are coordinates of i'th point. Initialze area.
Calculate value of shoelace formula. Return absolute value. Driver program to test above function. This code is contributed by. WriteLine polygonArea X, Y, n. Load Comments.During these challenging times, we guarantee we will work tirelessly to support you. We will continue to give you accurate and timely information throughout the crisis, and we will deliver on our mission — to help everyone in the world learn how to do anything — no matter what.
Thank you to our community and to all of our readers who are working to aid others in this time of crisis, and to all of those who are making personal sacrifices for the good of their communities. We will get through this together. Updated: May 10, References. Calculating the area of a polygon can be as simple as finding the area of a regular triangle or as complicated as finding the area of an irregular eleven-sided shape.
If you want to know how to find the area of a variety of polygons, just follow these steps. To calculate the area of a regular polygon, use the specific formula for that shape. For a square or rectangle, multiply the length of the base by the height. To find the length of a trapezoid, add the length of the two bases and multiply that number by the height.
Then, divide the total by 2 to get the area. Be sure to include units in your answer if they're provided. If you need to calculate the area of an irregularly-shaped polygon, keep reading to learn how!
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. I have a Polygon named as poly. I attempted to randomly select 5 coordinate points that lies inside the polygon. Although, this could miss frequently in a polygon with a large bounding box relative to its area.
My approach is to select x randomly within the polygon, then constrain y. Rejection sampling was proposed in comments on the other answer. The problem with rejection sampling is that the area of a polygon can be an arbitrarily small fraction of its bounding box, for example:. Pick a random point uniformly in the triangleas follows:. Here's one possible implementation, using shapely.
Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Generate sample coordinates inside a Polygon Ask Question.
Asked 5 years, 5 months ago. Active 1 year, 6 months ago. Viewed 8k times.
Are there any ideas for a straight and elegant implementation? Borys Borys 1 1 silver badge 5 5 bronze badges. Active Oldest Votes. This was my original idea, but it didn't look so good in the morning. Firstly, work out how to select one point within a polygon. This approach doesn't require NumPy and always produces a point within the polygon. Find the bounding box of the polygon.
Generate a uniform sample in the bounding box 3. If sample is inside polygon, return sample, else go to 2. A more robust approach is as follows: Triangulate the polygon and calculate the area of each triangle. Gareth Rees Gareth Rees Val Val 1. You have presented an alternative solution, but haven't reviewed the code.
Please explain your reasoning how your solution works and why it is better than the original so that the author and other readers can learn from your thought process.
I came up with some simple code to create a simple polygon from a list of coordinates, but other users on GIS StackExchange helped to improve the code. The code is working perfectly in Python 3. Possibly this code helps others and it would be nice to have suggestions how to create a better Python script that creates a polygon from coordinates in GeoPandas. I added improvements by ImanolUr, gene and reevesii to the code! I made a stupid mistake to reverse latitude and longitude order.
The location of the polygon can be checked by using folium in Jupyter Notebook:. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 1 year, 7 months ago. Active 9 months ago. Viewed 21k times.
Map [ GeoJson polygon. Davma Davma 1 1 gold badge 1 1 silver badge 8 8 bronze badges. If this is a working code, it seems more suited for Code Review stack codereview. Active Oldest Votes.