geojsonrgeo

Calculating area with RGeo and Geojson


I have a multi-polygon defined in geojson. I am trying to calculate its area.

When I traverse the ostensible code path, I am getting very confusing results. I suspect this is because I am using some element incorrectly.

boundaries = {...valid geojson...}
field_feature_collection = RGeo::GeoJSON.decode(boundaries, geo_factory: RGeo::Geographic.simple_mercator_factory)
first_feature = field_feature_collection[0]
first_feature.geometry.area
# => 1034773.6727743163 

I know the area of my feature is ~602982 square meters. I cannot figure out how to reconcile that with the result above.

I suspect I am missing some obvious projection.

Anyone have an idea on what's wrong?


Solution

  • This is most likely due to using the simple_mercator_factory as your geo_factory in the GeoJSON decoder. The simple_mercator_factory parses geometries from lon/lat coordinate systems but performs all of the calculations (length, area, etc.) using the Mercator projection (EPSG:3857) of the geometry.

    The Mercator projection gets more distorted the further away you get from the equator, so that's why the area you're getting is about double what you expect it to be.

    Depending on the scope of your project, it may be more appropriate to use a different projection, especially if area is a key metric for you. If you choose a different projection, the rgeo-proj4 gem has just been updated to work with newer versions of PROJ, so you can create the appropriate transformations in your app.

    Another option is to use the RGeo::Geographic.spherical_factory which uses a spherical approximation for the earth and will give you more realistic areas than the Mercator projection, but it is slower than using GEOS backed cartesian factories (which is what the simple_mercator_factory uses).