Differences between PostGIS and GEOS

51 views Asked by At

I am used to PostGIS, and I am trying to use the library (almost) directly, via perl and Geo::Geos.

As far as I know, GEOS is the library underlying PstGIS, so I am puzzled by the differences in buffering a polyline, as exemplified here:

use Mojo::Pg;
use Mojo::Util qw/dumper/;
use Geo::Geos::PrecisionModel qw/TYPE_FLOATING/;
use Geo::Geos::GeometryFactory;
use Geo::Geos::WKTReader;

$\ = "\n"; $, = "\t"; binmode(STDOUT, ":utf8");

my $pg = Mojo::Pg->new('postgresql://mydb:[email protected]/foo');

# Everything ok here
print $pg->db->query(q[SELECT ST_AsEWKT(ST_SetSRID(ST_LineFromEncodedPolyline('_p~iF~ps|U_ulLnnqC_mqNvxq`@'), 4326)::geography) as line])
    ->expand->hashes->first->{line} =~ s/.+?;//r;


# this is my expected reference
print $pg->db->query(q[SELECT ST_AsEWKT(ST_Buffer(ST_SetSRID(ST_LineFromEncodedPolyline('_p~iF~ps|U_ulLnnqC_mqNvxq`@'), 4326)::geography, 5000)::geometry) as line])
    ->hashes->first->{line}  =~ s/.+?;//r;;

my $pm   = Geo::Geos::PrecisionModel->new(TYPE_FLOATING);
my $gf   = Geo::Geos::GeometryFactory::create($pm, 4326);

# this works ok
my $line = Geo::Geos::WKTReader::read("LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)", $gf);
print $line;

# THis is an unexpected result
print $line->buffer(5000);

With the results being:

LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)
POLYGON((-121.001406457226 40.6719556418545,-126.483971110947 43.2131177473093,-126.493800934598 43.2182233605429,-126.502065444074 43.2246264448632,-126.508447019926 43.2320813087531,-126.512700015374 43.240301816154,-126.514660257786 43.248972329225,-126.514251468824 43.2577597967163,-126.511488339756 43.2663265297307,-126.506476121921 43.274343177481,-126.499406724466 43.2815014046563,-126.490551447548 43.2875257801147,-126.480250613625 43.2921844140233,-126.468900486462 43.2952979266666,-126.456937980813 43.2967463954839,-126.444823759832 43.296474005162,-126.4330243869 43.2944912157185,-126.421994240005 43.29087436171,-120.916800718113 40.7373584074441,-120.908636764637 40.7323216412899,-120.901783885625 40.7262615857863,-120.896458889026 40.7193702991453,-120.892829996089 40.7118661224248,-120.144505808917 38.5114481318421,-120.142794200977 38.5027245484579,-120.143281319239 38.4938967846813,-120.145947624185 38.4853040763681,-120.150689910778 38.4772765010062,-120.157325387088 38.4701223137339,-120.165598782058 38.4641161378514,-120.175192196723 38.4594884580769,-120.185737314132 38.456416813189,-120.196829499843 38.4550190189286,-120.208043259316 38.4553486746909,-120.218948472213 38.457393121498,-120.229126797468 38.4610739270376,-120.238187637482 38.4662498793455,-120.245783064986 38.4727223771714,-120.251621151615 38.4802430153764,-120.255477192433 38.4885230809388,-121.001406457226 40.6719556418545))
LINESTRING (-120.2000000000000028 38.5000000000000000, -120.9500000000000028 40.7000000000000028, -126.4530000000000030 43.2520000000000024)
POLYGON ((-121.3542793773466428 40.3363342669942355, -126.6633546239553425 42.7984022352561624, -126.7478052528298349 42.8481561404404729, -126.8209266811667533 42.9134295386694049, -126.8799088934821100 42.9917140098547677, -126.9224852364341984 43.0800011256713447, -126.9470195250404885 43.1748980617702998, -126.9525689203488099 43.2727579821161541, -126.9389201622095982 43.3698201848504894, -126.9065977647438359 43.4623546239553491, -126.8568438595595325 43.5468052528298344, -126.7915704613306076 43.6199266811667599, -126.7132859901452377 43.6789088934821024, -126.6249988743286536 43.7214852364341979, -126.5301019382297056 43.7460195250404809, -126.4322420178838513 43.7515689203488023, -126.3351798151495160 43.7379201622095977, -126.2426453760446634 43.7055977647438425, -120.7396453760446633 41.1535977647438429, -120.6529159360074601 41.1021704351660873, -120.5782680326100689 41.0343880147678846, -120.5187373769164054 40.9530070155767447, -120.4767449059107349 40.8613369638940753, -119.7267449059107349 38.6613369638940725, -119.7043630896021966 38.5659094306706010, -119.7010283218038751 38.4679490349888979, -119.7168687557720830 38.3712203399181817, -119.7512756525128594 38.2794405749639424, -119.8029267742800386 38.1961367850226949, -119.8698371974304138 38.1245102880245668, -119.9494355919261039 38.0673136500805995, -120.0386630361059304 38.0267449059107321, -120.1340905693294019 38.0043630896022009, -120.2320509650111120 38.0010283218038722, -120.3287796600818211 38.0168687557720872, -120.4205594250360605 38.0512756525128566, -120.5038632149773150 38.1029267742800357, -120.5754897119754361 38.1698371974304109, -120.6326863499194104 38.2494355919261011, -120.6732550940892708 38.3386630361059275, -121.3542793773466428 40.3363342669942355))

I'm kind of guessing that there is a conversion issue with the distance, but I can't find documentation about it.

What am I doing wrong?

0

There are 0 answers