In my model I have a polygon field defined via
polygon = models.PolygonField(srid=4326, geography=True, null=True, blank=True)
When I want to determine the area of the polygon, I call
area_square_degrees = object.polygon.area
But how can I convert the result in square degrees into m2 with GeoDjango?
This answer does not work, since area
does not have a method sq_m
. Is there any built-in conversion?
The area of a polygon is the total space covered by a polygon. A polygon can be regular and irregular, and thus to find its area, we have to use different methods depending upon the shape of the polygon. What is Area? A polygon is a closed plane figure bounded by straight line segments.
GeoDjango is an included contrib module in Django, but you need to install a few spatial libraries for it to work. Follow this installation guide. I'm using PostGIS which is an extension for PostgreSQL and allows you to store geometry objects like the points and polygons we talked about earlier in a database, and perform spatial queries on them.
To perform a point in polygon search, first you need a point. I'm using the point from the earlier example, which is the latitude and longitude coordinate pair for Times Square. We'll do this in the Django shell interpreter for now, but in a real project it could be part of a view or something like that.
We can use the apothem area formula of a polygon to calculate the length of the apothem. A = 1 2 × a × P, where, A is the polygon area, a is the apothem, and P is the perimeter. An irregular polygon is a polygon with interior angles of different measures.
You need to transform your data to the correct spatial reference system.
area_square_local_units = object.polygon.transform(srid, clone=False).area
In the UK you might use the British National Grid SRID of 27700 which uses meters.
area_square_meters = object.polygon.transform(27700, clone=False).area
You may or may not want to clone the geometry depending on whether or not you need to do anything else with it in its untransformed state.
Docs are here https://docs.djangoproject.com/en/1.8/ref/contrib/gis/geos/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With