Multipart to singlepart

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Multipart to singlepart

iomeneandrei
Hi all,
is there in OGR a command that I can use to convert multipart polygons in singleparts?

Thank you,

a
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Even Rouault
Le lundi 06 décembre 2010 12:54:28, iomeneandrei a écrit :
> Hi all,
> is there in OGR a command that I can use to convert multipart polygons in
> singleparts?

None that I am aware of, but if you have some scripting/programming abilities,
you could do that by using the OGR API.

>
> Thank you,
>
> a
>
> -----
> Andrea Borruso
>
> ----------------------------------------------------
> email: [hidden email]
> website: http://blog.spaziogis.it
> my 2.0 life: http://aborruso.spaziogis.it
> feed: http://feeds2.feedburner.com/Tanto
> 38° 7' 48" N, 13° 21' 9" E
> ----------------------------------------------------
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Frank Warmerdam
In reply to this post by iomeneandrei
On 10-12-06 06:54 AM, iomeneandrei wrote:
>
> Hi all,
> is there in OGR a command that I can use to convert multipart polygons in
> singleparts?

Andrea,

At the API or scripting level there is the OGRGeometryFactory::forceToPolygon()
method to try and convert a multipolygon to a simple polygon.  I am not
aware of a way to do this from the commandline utilities.

/************************************************************************/
/*                           forceToPolygon()                           */
/************************************************************************/

/**
  * \brief Convert to polygon.
  *
  * Tries to force the provided geometry to be a polygon.  Currently
  * this just effects a change on multipolygons.  The passed in geometry is
  * consumed and a new one returned (or potentially the same one).
  *
  * @param poGeom the input geometry - ownership is passed to the method.
  * @return new geometry.
  */

OGRGeometry *OGRGeometryFactory::forceToPolygon( OGRGeometry *poGeom )

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

iomeneandrei
Hi Frank,
Frank Warmerdam wrote
At the API or scripting level there is the OGRGeometryFactory::forceToPolygon()
method to try and convert a multipolygon to a simple polygon.  
I will try using OGR API. I will be slow, but I hope I'll be able to do it.

Thank you very much,

a
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

iomeneandrei
In reply to this post by Frank Warmerdam
Hi Frank,
Frank Warmerdam wrote
I am not aware of a way to do this from the commandline utilities.
could I use the -nlt parameter of ogr2ogr and force POLYGON? If no, what's the "power" of -nlt parameter?

Once again, thank you,

a
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Frank Warmerdam
On 10-12-06 06:13 PM, iomeneandrei wrote:
>
> Hi Frank,
>
> Frank Warmerdam wrote:
>>
>> I am not aware of a way to do this from the commandline utilities.
>>
> could I use the -nlt parameter of ogr2ogr and force POLYGON? If no, what's
> the "power" of -nlt parameter?

Andrea,

Reviewing the ogr2ogr code I see this does trigger a call to forceToPolygon()
though *mostly* it exists to set the type of the created layer.  So, yes,
-nlt polygon with ogr2ogr may well do what you want.

Note that in a format like Shapefile there is no clear distinction
between polygon and multipolygon, so the geometries are reclassified on
read.  So -nlt polygon to a shapefile and then reading it back with ogr
will still result in multipolygons if that is the nature of the rings.

You should have better luck with a more simple features oriented format
like GML.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

iomeneandrei
Hi Frank,
Frank Warmerdam wrote
Reviewing the ogr2ogr code I see this does trigger a call to forceToPolygon()
though *mostly* it exists to set the type of the created layer.  So, yes,
-nlt polygon with ogr2ogr may well do what you want.
I have used this GML file as input:
https://gist.github.com/731590

It contains a multipolygon.

Then I run this command: ogr2ogr -f GML -nlt POLYGON  poly_output.gml poly_input.gml

The output file contains the same multipolygon. Is it normal?

I'm using FWTools 2.4.7 for Windows.

Thank you very much,

Andrea
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Frank Warmerdam
On 10-12-07 03:43 AM, iomeneandrei wrote:

>
> Hi Frank,
>
> Frank Warmerdam wrote:
>>
>> Reviewing the ogr2ogr code I see this does trigger a call to
>> forceToPolygon()
>> though *mostly* it exists to set the type of the created layer.  So, yes,
>> -nlt polygon with ogr2ogr may well do what you want.
>>
> I have used this GML file as input:
> https://gist.github.com/731590
>
> It contains a multipolygon.
>
> Then I run this command: ogr2ogr -f GML -nlt POLYGON  poly_output.gml
> poly_input.gml
>
> The output file contains the same multipolygon. Is it normal?
>
> I'm using FWTools 2.4.7 for Windows.

Andrea,

I tried it with a trunk build and the multipolygon is converted to a polygon.
I suspect the feature of the forcing support is new and if you want it you
would need a reasonably recent development build - more recent than FWTools.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

iomeneandrei
Dear Frank,
Frank Warmerdam wrote
I tried it with a trunk build and the multipolygon is converted to a polygon.
I suspect the feature of the forcing support is new and if you want it you
would need a reasonably recent development build - more recent than FWTools.
I have downloaded GDAL 1.7.3 from here http://vbkto.dyndns.org/sdk/, and I have made a test. The output file now does not contain ever MultiPolygon, but it has only Polygon. And I have made this test using two output formats: SQLite and GML. This is a GML output:
https://gist.github.com/732281

But I have a question for you: is it a real multpart to singlepart transformation? The output file is without any MultiPolygon, but the two polygons that constitute the former MultiPolygon remain in some way geometrically related. I was thinking of getting two separate polygons, but if I will select one of them in a desktop GIS, also the other one is selected.
Probably it's correct, but it's not what I want.

It's really a pleasure to "talk" with you.

Thank you,

Andrea
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Frank Warmerdam
On 10-12-07 02:48 PM, iomeneandrei wrote:
> I have downloaded GDAL 1.7.3 from here http://vbkto.dyndns.org/sdk/, and I
> have made a test. The output file now does not contain ever MultiPolygon,
> but it has only Polygon. And I have made this test using two output formats:
> SQLite and GML. This is a GML output:
> https://gist.github.com/732281

Andrea,

Good.

> But I have a question for you: is it a real multpart to singlepart
> transformation? The output file is without any MultiPolygon, but the two
> polygons that constitute the former MultiPolygon remain in some way
> geometrically related. I was thinking of getting two separate polygons, but
> if I will select one of them in a desktop GIS, also the other one is
> selected.
> Probably it's correct, but it's not what I want.
>
> It's really a pleasure to "talk" with you.

I'm afraid it is just pushing all the rings into one polygon feature which
is in truth not a valid simple feature polygon geometry.  If you want
multipolygons split into multiple features - a reasonable wish - you will
need another mechanism.  You could do this in your own script or program
but I am not aware of mechanism to do this with ogr2ogr or other existing
OGR utilities.  It might be nice to have a -explode-collections option
for ogr2ogr what would produce one feature for each geometry in any kind
of geometry collection in the source file.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] Re: Multipart to singlepart

Rahkonen Jukka (Tike)
In reply to this post by iomeneandrei
iomeneandrei <aborruso <at> tin.it> writes:

>
>
> Hi all,
> is there in OGR a command that I can use to convert multipart polygons in
> singleparts?

If it does not matter to use some other tool than OGR, OpenJUMP can explode
multifeatures and geometry collections. I made a little test and it correctly
exploded a multipolygon with two polygons containing holes into two simple
polygons with holes. Functionality is behind the right-click context menu.

-Jukka Rahkonen-

_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: [gdal-dev] Re: Multipart to singlepart

iomeneandrei
Hi Jukka,
thank you for your time and for your advice.
Rahkonen Jukka wrote
If it does not matter to use some other tool than OGR, OpenJUMP can explode
multifeatures and geometry collections.
My goal is to make this transformation in Python, and I did not want to write either unnecessary code or complicated code (for me).
If I have a solid and interesting result, I will publish it here.

Best regards,

Andrea
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Paolo Corti
> My goal is to make this transformation in Python, and I did not want to
> write either unnecessary code or complicated code (for me).
> If I have a solid and interesting result, I will publish it here.

Ciao Andrea
this code will do the trick (note that it will create a polygon for
each ring, so you will get a polygon for each hole as well)

import os
from osgeo import ogr

def multipoly2poly(in_lyr, out_lyr):
    for in_feat in in_lyr:
        geom = in_feat.GetGeometryRef()
        if geom.GetGeometryName() == 'MULTIPOLYGON':
            for geom_part in geom:
                addPolygon(geom_part.ExportToWkb(), out_lyr)
        else:
            addPolygon(geom.ExportToWkb(), out_lyr)

def addPolygon(simplePolygon, out_lyr):
    featureDefn = out_lyr.GetLayerDefn()
    polygon = ogr.CreateGeometryFromWkb(simplePolygon)
    out_feat = ogr.Feature(featureDefn)
    out_feat.SetGeometry(polygon)
    out_lyr.CreateFeature(out_feat)
    print 'Polygon added.'

from osgeo import gdal
gdal.UseExceptions()
driver = ogr.GetDriverByName('ESRI Shapefile')
in_ds = driver.Open('data/multipoly.shp', 0)
in_lyr = in_ds.GetLayer()
outputshp = 'data/poly.shp'
if os.path.exists(outputshp):
    driver.DeleteDataSource(outputshp)
out_ds = driver.CreateDataSource(outputshp)
out_lyr = out_ds.CreateLayer('poly', geom_type=ogr.wkbPolygon)
multipoly2poly(in_lyr, out_lyr)

--
Paolo Corti
GIS specialist and web developer
web: http://www.paolocorti.net
twitter: @paolo_corti
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

iomeneandrei
Caro Paolo,
Paolo Corti wrote
Ciao Andrea
this code will do the trick (note that it will create a polygon for
each ring, so you will get a polygon for each hole as well)
[...]
writing in groups like this is always a surprise and a great pleasure. Thank you, thank you, thank you.

I have modified a little your code - https://gist.github.com/735330 - and I have this message:
Polygon added.
Traceback (most recent call last):
  File "PaoloIsGreat.py ", line 32, in <module> multipoly2poly(in_lyr, out_lyr)
  File "PaoloIsGreat.py ", line 8, in multipoly2poly for geom_part in geom:
TypeError: 'Geometry' object is not iterable

It seems that the object I'm trying to iterate is actually None. This is my input file:
poly.zip

As output I obtain only one polygon, and I should have three.

What do you think about?

Writing in groups like this is always a surprise and a great pleasure.

ciao,

a
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

Even Rouault
>
> I have modified a little your code - https://gist.github.com/735330 - and I
> have this message:
> Polygon added.
> Traceback (most recent call last):
>   File "PaoloIsGreat.py ", line 32, in <module> multipoly2poly(in_lyr,
> out_lyr)
>   File "PaoloIsGreat.py ", line 8, in multipoly2poly for geom_part in geom:
> TypeError: 'Geometry' object is not iterable

Geometry have been made iterable only since GDAL 1.7.0. You need to upgrade
your GDAL version or modify a bit the script to make the iteration with the
OGR API.

instead of :

for geom_part in geom:
        addPolygon(geom_part.ExportToWkb(), out_lyr)

try :

for i in range(geom.GetGeometryCount()):
        geom_part = geom.GetGeometryRef(i)
        addPolygon(geom_part.ExportToWkb(), out_lyr)
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

iomeneandrei
Hi Even,
Even Rouault wrote
Geometry have been made iterable only since GDAL 1.7.0. You need to upgrade
your GDAL version or modify a bit the script to make the iteration with the
OGR API.

instead of :

for geom_part in geom:
        addPolygon(geom_part.ExportToWkb(), out_lyr)

try :

for i in range(geom.GetGeometryCount()):
        geom_part = geom.GetGeometryRef(i)
        addPolygon(geom_part.ExportToWkb(), out_lyr)
now it works perfectly!!

Thank you very much,

Andrea
Reply | Threaded
Open this post in threaded view
|

Re: Multipart to singlepart

sebastian.ovide
Hi Andrea,

Just a note of warning: 

if you have a point x,y that is inside the hole (so it doesn't belong the the polygon), and you run some point in polygon query (intersection for example) you will get that it is no part of any polygon.... but if you convert the ring in two polygons using this method.... that point will become part of a polygon (the ex hole)... So if for example the polygon represent the field, the hole represent the water and the point is a boat, after the conversion (ring to more polygons) the boat would be drained.... ;)

Not sure if that is what you need....

On Thu, Dec 9, 2010 at 9:58 PM, iomeneandrei <[hidden email]> wrote:

Hi Even,

Even Rouault wrote:
>
> Geometry have been made iterable only since GDAL 1.7.0. You need to
> upgrade
> your GDAL version or modify a bit the script to make the iteration with
> the
> OGR API.
>
> instead of :
>
> for geom_part in geom:
>       addPolygon(geom_part.ExportToWkb(), out_lyr)
>
> try :
>
> for i in range(geom.GetGeometryCount()):
>       geom_part = geom.GetGeometryRef(i)
>       addPolygon(geom_part.ExportToWkb(), out_lyr)
>

now it works perfectly!!

Thank you very much,

Andrea

-----
Andrea Borruso

----------------------------------------------------
email: [hidden email]
website: http://blog.spaziogis.it
my 2.0 life: http://aborruso.spaziogis.it
feed: http://feeds2.feedburner.com/Tanto
38° 7' 48" N, 13° 21' 9" E
----------------------------------------------------
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Multipart-to-singlepart-tp5807508p5820973.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.
_______________________________________________



--
Sebastian E. Ovide





_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev