Marco Fedele Marco Fedele - 1 year ago 99
Python Question

GeoDjango polygon area

I have implemented GeoDjango using postgis.

Here is my model:

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

When I call data.area it returns a float, but I don't have any clues about it's measurement units, and it's a problem because I want to test if it's bigger of a pre-set area in squared meters.

Can you help me?

Answer Source

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

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) 
    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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download