Search code examples
postgresqlpostgis

How to insert a GeoJSON multipolygon into a PostGIS table?


I'm new with postgis and I don't know why when I insert into database a mulipolygon, the value is empty.

I'm using postgressql with postgis plugin.

I create my table with this code: CREATE TABLE IF NOT EXISTS countries ( table_id SERIAL, properties jsonb not null, geom geometry(GeometryZ,4326), primary key (table_id));

And I read a file an insert feature by feature. All features that are not multipolygon are inserted well but, when I try to insert a multipolygon the value that inserts is empty.

And I've tryied to insert this exaple and it works:

INSERT INTO test ( properties, geom ) VALUES ( '{"name":"Argentina"}', ST_Force3D(ST_SetSRID(ST_GeomFromGeoJSON('{ 
  "type": "MultiPolygon",
  "coordinates": 
    [[[[-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25]]],
    [[[-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25]],
    [[-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25], [-66.45, -55.25]]]]
}'), 4326) ) );

But when I try to insert a real value from file insert an empty value and I dont know why.

INSERT INTO test ( properties, geom ) VALUES ( '{"name":"Argentina"}', ST_Force3D(ST_SetSRID(ST_GeomFromGeoJSON('{ 
  "type": "MultiPolygon",
  "coordinates": [[[ [-65.5, -55.2],[-66.45, -55.25],[-66.95992, -54.89681],[-67.56244, -54.87001],[-68.63335, -54.8695],[-68.63401, -52.63637],[-68.25, -53.1],[-67.75, -53.85],[-66.45, -54.45],[-65.05, -54.7],[-65.5, -55.2]]],[[[-64.964892,-22.075862],[-64.377021,-22.798091],[-63.986838,-21.993644],[-62.846468,-22.034985],[-62.685057,-22.249029],[-60.846565,-23.880713],[-60.028966,-24.032796],[-58.807128,-24.771459],[-57.777217,-25.16234],[-57.63366,-25.603657],[-58.618174,-27.123719],[-57.60976,-27.395899],[-56.486702,-27.548499],[-55.695846,-27.387837],[-54.788795,-26.621786],[-54.625291,-25.739255],[-54.13005,-25.547639],[-53.628349,-26.124865],[-53.648735,-26.923473],[-54.490725,-27.474757],[-55.162286,-27.881915],[-56.2909,-28.852761],[-57.625133,-30.216295],[-57.874937,-31.016556],[-58.14244,-32.044504],[-58.132648,-33.040567],[-58.349611,-33.263189],[-58.427074,-33.909454],[-58.495442,-34.43149],[-57.22583,-35.288027],[-57.362359,-35.97739],[-56.737487,-36.413126],[-56.788285,-36.901572],[-57.749157,-38.183871],[-59.231857,-38.72022],[-61.237445,-38.928425],[-62.335957,-38.827707],[-62.125763,-39.424105],[-62.330531,-40.172586],[-62.145994,-40.676897],[-62.745803,-41.028761],[-63.770495,-41.166789],[-64.73209,-40.802677],[-65.118035,-41.064315],[-64.978561,-42.058001],[-64.303408,-42.359016],[-63.755948,-42.043687],[-63.458059,-42.563138],[-64.378804,-42.873558],[-65.181804,-43.495381],[-65.328823,-44.501366],[-65.565269,-45.036786],[-66.509966,-45.039628],[-67.293794,-45.551896],[-67.580546,-46.301773],[-66.597066,-47.033925],[-65.641027,-47.236135],[-65.985088,-48.133289],[-67.166179,-48.697337],[-67.816088,-49.869669],[-68.728745,-50.264218],[-69.138539,-50.73251],[-68.815561,-51.771104],[-68.149995,-52.349983],[-68.571545,-52.299444],[-69.498362,-52.142761],[-71.914804,-52.009022],[-72.329404,-51.425956],[-72.309974,-50.67701],[-72.975747,-50.74145],[-73.328051,-50.378785],[-73.415436,-49.318436],[-72.648247,-48.878618],[-72.331161,-48.244238],[-72.447355,-47.738533],[-71.917258,-46.884838],[-71.552009,-45.560733],[-71.659316,-44.973689],[-71.222779,-44.784243],[-71.329801,-44.407522],[-71.793623,-44.207172],[-71.464056,-43.787611],[-71.915424,-43.408565],[-72.148898,-42.254888],[-71.746804,-42.051386],[-71.915734,-40.832339],[-71.680761,-39.808164],[-71.413517,-38.916022],[-70.814664,-38.552995],[-71.118625,-37.576827],[-71.121881,-36.658124],[-70.364769,-36.005089],[-70.388049,-35.169688],[-69.817309,-34.193571],[-69.814777,-33.273886],[-70.074399,-33.09121],[-70.535069,-31.36501],[-69.919008,-30.336339],[-70.01355,-29.367923],[-69.65613,-28.459141],[-69.001235,-27.521214],[-68.295542,-26.89934],[-68.5948,-26.506909],[-68.386001,-26.185016],[-68.417653,-24.518555],[-67.328443,-24.025303],[-66.985234,-22.986349],[-67.106674,-22.735925],[-66.273339,-21.83231],[-64.964892,-22.075862] ]]]}},
}'), 4326) ) );

Here is the file: https://pastebin.com/MRcQDV5s

EDIT:

It works!

This function works perfectly.

CREATE TABLE IF NOT EXISTS test ( table_id SERIAL, properties jsonb not null, geom geometry(GeometryZ,4326), primary key (table_id))

INSERT INTO test ( properties, geom ) VALUES ( '" + property + "', ST_Force3D(ST_SetSRID(ST_GeomFromGeoJSON('" + geometry + "'), 4326) ));

Each different type is stored perfectly.

The problem was pgAdmin version that doesnt show data in database.


Solution

  • The example geometry is an invalid one, so its (un)usability with any PostGIS function is not trustable.

    Ignoring the Z component, the column in the table is a simple polygon, while the input is a multi polygon. Depending on your needs, you have two options

    1) change the table definition so it can store multipolygons and use the current query to populate it (geometry(MultiPolygonZ,4326))

    2) keep the current table definition, but split multi-parts into single parts. That is, if a source polygon has 3 parts, it would create 3 records in the table. Use st_dump for this.

    INSERT INTO test ( properties, geom ) 
    VALUES ( '{"name":"Argentina"}', 
    ST_Force3D(
      (ST_DUMP(
        ST_SetSRID( 
            ST_GeomFromGeoJSON('{...}'),
        4326)
      )).geom ) 
    );