[postgis-users] ST_AsGeoJson output geometry type not the same as input

Sandro Santilli strk at keybit.net
Wed Sep 3 00:55:47 PDT 2014


On Tue, Sep 02, 2014 at 01:19:55PM -0400, Max Demars wrote:
> Hi list,
> 
> I am using postgis to split a multipolygon feature by a linestring. Both
> feature are geojson inputs.
> 
> However the outputs returns a geojson that is a geocollection of two
> polygon features. In fact this is correct, but for consistency I would like
> to keep the the splitted features into a single Multipolygon (even if there
> is only one linear ring in it). Is it possible?

Possible, but such a multipolygon would be invalid.

> This is my query:
> 
> SQLQuery = '''SELECT
> ST_AsGeoJSON(ST_Split(ST_GeomFromGeoJSON('%s'),ST_GeomFromGeoJSON('%s')))::json
> As f''' % (feature,blade)

Make it:

SELECT
ST_AsGeoJSON(
 ST_Collect(geom) FROM (
  SELECT (
    ST_Dump(
      ST_Split(ST_GeomFromGeoJSON('%s'), ST_GeomFromGeoJSON('%s'))
    )
  ).geom
 ) foo
)::json As f

ST_Collect will create a MULTIPOLYGON if all inputs are of the POLYGON type.

--strk;

 ()  ASCII ribbon campaign  --  Keep it simple !
 /\  http://strk.keybit.net/rants/ascii_mails.txt  


More information about the postgis-users mailing list