Marco Fedele - 6 months ago 38

Python Question

I have implemented GeoDjango using postgis.

Here is my model:

`...`

geometria = models.PolygonField(srid=4326, null=True)

...

When I call

Can you help me?

Answer

If you are dealing with large areas on the map, you should set

```
geometria = models.PolygonField(srid=4326, null=True, geography=True)
```

As mentioned in geodjango's documentation https://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/#geography

Geography Type In PostGIS 1.5, the geography type was introduced -- it provides native support for spatial features represented with geographic coordinates (e.g., WGS84 longitude/latitude). [7] Unlike the plane used by a geometry type, the geography type uses a spherical representation of its data. Distance and measurement operations performed on a geography column automatically employ great circle arc calculations and return linear units. In other words, when ST_Distance is called on two geographies, a value in meters is returned (as opposed to degrees if called on a geometry column in WGS84).

If you do not have `geography=True`

, we are storing things as plain geometries, we will need to do conversion from `square degrees`

(the floating point result you are getting) into a unit of measure you prefer because we cannot calculate area from geographic coordinates. We can instead add a helper method which is in a projected coordinate space to do the transformation:

```
def get_acres(self):
"""
Returns the area in acres.
"""
# Convert our geographic polygons (in WGS84)
# into a local projection for New York (here EPSG:32118)
self.polygon.transform(32118)
meters_sq = self.polygon.area.sq_m
acres = meters_sq * 0.000247105381 # meters^2 to acres
return acres
```

Which projection we use depends on the extent of the data, and how accurate we need the results: here I've illustrated with a specific projection for part of New York, but if your data isn't particularly accurate, you could easily substitute a global projection or just use a simple formula.