CartoDB / crankshaft

CARTO Spatial Analysis extension for PostgreSQL
BSD 3-Clause "New" or "Revised" License
54 stars 20 forks source link

Invalid geometries generated with Area of Intereset #187

Closed Algunenano closed 6 years ago

Algunenano commented 6 years ago

When using Area of Interest analysis with points close to the map margins (~180º longitude) invalid polygons are generated, covering the whole world: image

Carto file with an example: carto.zip

The query to generate the analysis was:

CREATE TABLE analysis_2f13a3dbd7_d79a52e91998c27b34e4665ff11165508de12e92 AS
SELECT * FROM (SELECT
ST_Buffer(the_geom::geography, 8000)::geometry the_geom,cartodb_id,magnst,nst,magsource,locationsource,status,type,place,id,net,magtype,magerror,deptherror,horizontalerror,rms,dmin,gap,mag,depth,longitude,latitude,updated,time
FROM (SELECT * FROM all_month) _camshaft_buffer) _analysis_create_table_query
LIMIT 0

An example from that dataset with the issue: Point in the dataset:

Select ST_AsEWKT("the_geom_webmercator"),latitude,longitude from all_month where cartodb_id = 3139;
                      st_asewkt                       | latitude | longitude 
------------------------------------------------------+----------+-----------
 SRID=3857;POINT(-20033901.5912875 -2815360.03290273) | -24.5074 | -179.9676

Point after buffer (invalid render both in builder and in Qgis):

Select ST_AsEWKT("the_geom"),latitude,longitude from analysis_2f13a3dbd7_d79a52e91998c27b34e4665ff11165508de12e92 where cartodb_id = 3139;

                            st_asewkt                                                                                                                                                                                                                                                     

                                                                  | latitude | longitude 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------+----------+-----------
 SRID=4326;POLYGON((-179.888736236903 -24.5089311174815,-179.890574015504 -24.522980009554,-179.895373178666 -24.5364307183352,-179.90295004877 -24.5487659985138,-179.913013887712 -24.5595113765843,-179.925178002368 -24.5682534509015,-179.938974595903 -24.5746558492224,-179.9538727
95839 -24.5784722196264,-179.969299158455 -24.5795557456198,-179.984659845753 -24.5778648122829,-179.999363601243 -24.5734646019158,179.987155382308 -24.5665245583696,179.975415604496 -24.5573118222547,179.965868265902 -24.5461808975537,179.958879922004 -24.5335599571771,179.954718
449798 -24.5199343247056,179.953542858056 -24.5058277768944,179.955397316563 -24.491782392564,179.960209612269 -24.4783377255956,179.967794068248 -24.4660101014935,179.977858791506 -24.4552728281988,179.990016954279 -24.4465380734785,-179.996198334157 -24.4401410952308,-179.9813161
3736 -24.4363274202147,-179.965907462713 -24.435243454572,-179.950563455368 -24.4369308801543,-179.935872832433 -24.4413250486881,-179.922399401827 -24.4482574361412,-179.910660508275 -24.4574620674774,-179.901107217752 -24.4685856725939,-179.894106991872 -24.4812011929179,-179.889
929516558 -24.4948241300181,-179.888736236903 -24.5089311174815)) | -24.5074 | -179.9676
rafatower commented 6 years ago

I'm pretty sure the "offending" query is generated in camshaft: https://github.com/CartoDB/camshaft/blob/99d6e5d8b341f8eed1b4c4c576b3e305e3ff19f0/lib/node/nodes/buffer.js#L47-L65

though I don't know how hard is to solve these "corner cases" around 180º long. It is kind of a generic problem with geometry types and postgis.

For this very instance the trick may be splitting the geography polygons around 180º before casting back to geometry. Take a look at this post: https://carto.com/blog/jets-and-datelines/

Algunenano commented 6 years ago

I'm pretty sure the "offending" query is generated in camshaft: https://github.com/CartoDB/camshaft/blob/99d6e5d8b341f8eed1b4c4c576b3e305e3ff19f0/lib/node/nodes/buffer.js#L47-L65 To be honest, I have the very same file open in my IDE but I opened it here by mistake. I'll create it there.

Algunenano commented 6 years ago

Migrated to https://github.com/CartoDB/camshaft/issues/339