Skip to content

Instantly share code, notes, and snippets.

@devdattaT
Created April 23, 2018 10:29
Show Gist options
  • Save devdattaT/89ee9cdcaac26875339c7c57a0c66d1b to your computer and use it in GitHub Desktop.
Save devdattaT/89ee9cdcaac26875339c7c57a0c66d1b to your computer and use it in GitHub Desktop.
Area Mismatch with PostGIS
var turf = require( "turf" );
let input='{"type":"Polygon","coordinates":[[[81.84447939698728,25.457757],[81.84430664903012,25.456003060556696],[81.84379504376571,25.45431652402258],[81.84296424189746,25.452762203050835],[81.84184617067527,25.45139982932473],[81.84048379694916,25.450281758102534],[81.83892947597742,25.449450956234294],[81.8372429394433,25.448939350969884],[81.835489,25.44876660301271],[81.83373506055669,25.448939350969884],[81.83204852402257,25.449450956234294],[81.83049420305083,25.450281758102534],[81.82913182932472,25.45139982932473],[81.82801375810253,25.452762203050835],[81.82718295623428,25.45431652402258],[81.82667135096987,25.456003060556696],[81.82649860301271,25.457757],[81.82667135096987,25.459510939443305],[81.82718295623428,25.46119747597742],[81.82801375810253,25.462751796949167],[81.82913182932472,25.464114170675273],[81.83049420305083,25.465232241897468],[81.83204852402257,25.466063043765708],[81.83373506055669,25.466574649030118],[81.835489,25.46674739698729],[81.8372429394433,25.466574649030118],[81.83892947597742,25.466063043765708],[81.84048379694916,25.465232241897468],[81.84184617067527,25.464114170675273],[81.84296424189746,25.462751796949167],[81.84379504376571,25.46119747597742],[81.84430664903012,25.459510939443305],[81.84447939698728,25.457757]]]}';
//read the geometry
var poly= JSON.parse(input);
var area = turf.area(poly);
console.log(area);
WITH dummy (geomSTR) as (
values ('{"type":"Polygon","coordinates":[[[81.84447939698728,25.457757],[81.84430664903012,25.456003060556696],[81.84379504376571,25.45431652402258],[81.84296424189746,25.452762203050835],[81.84184617067527,25.45139982932473],[81.84048379694916,25.450281758102534],[81.83892947597742,25.449450956234294],[81.8372429394433,25.448939350969884],[81.835489,25.44876660301271],[81.83373506055669,25.448939350969884],[81.83204852402257,25.449450956234294],[81.83049420305083,25.450281758102534],[81.82913182932472,25.45139982932473],[81.82801375810253,25.452762203050835],[81.82718295623428,25.45431652402258],[81.82667135096987,25.456003060556696],[81.82649860301271,25.457757],[81.82667135096987,25.459510939443305],[81.82718295623428,25.46119747597742],[81.82801375810253,25.462751796949167],[81.82913182932472,25.464114170675273],[81.83049420305083,25.465232241897468],[81.83204852402257,25.466063043765708],[81.83373506055669,25.466574649030118],[81.835489,25.46674739698729],[81.8372429394433,25.466574649030118],[81.83892947597742,25.466063043765708],[81.84048379694916,25.465232241897468],[81.84184617067527,25.464114170675273],[81.84296424189746,25.462751796949167],[81.84379504376571,25.46119747597742],[81.84430664903012,25.459510939443305],[81.84447939698728,25.457757]]]}')
)
SELECT ST_AREA(ST_GeomFromGeoJSON(geomSTR)::geography, true) as spheroidal_area,
ST_AREA(ST_GeomFromGeoJSON(geomSTR)::geography, false) as spherical_area from dummy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment