Create Multiple Polygons from Polygon and Polylines

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Create Multiple Polygons from Polygon and Polylines

Matthew Denno
Hi All,

I am trying to programmaticly create a polygon shapefile from two separate input shapefiles.  The first is a boundary polygon shapefile and the second is a a polyline shapefile that contains a number of segments (could many) that are spatially contained in the boundary polygon.  The polyline segments either form closed areas or when connected end-to-end extend accross the polygon. I want to use the polylines to essentially cut the polygon into smaller polygons.  Does anyone have any suggestions where to start with this problem.  My question may not be completely clear so I have uploaded an example picture: http://www.flickr.com/photos/42311742@N02/3904105994/

Thanks,

Matt

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

AW: Create Multiple Polygons from Polygon and Polylines

Hugentobler  Marco
Hi Matthew

For the polylines that cut the polygon, you could use
QgsVectorLayer::splitFeatures (or on feature/geometry level
QgsGeometry::splitGeometry
And for the features that are contained in the polygons, there is
QgsVectorLayer::addRing.

Hope it helps,
Marco


-----Ursprüngliche Nachricht-----
Von: [hidden email] im Auftrag von Matthew Denno
Gesendet: Mi 09.09.2009 16:53
An: qgis-developer
Betreff: [Qgis-developer] Create Multiple Polygons from Polygon and Polylines
 
Hi All,

I am trying to programmaticly create a polygon shapefile from two separate
input shapefiles.  The first is a boundary polygon shapefile and the second
is a a polyline shapefile that contains a number of segments (could many)
that are spatially contained in the boundary polygon.  The polyline segments
either form closed areas or when connected end-to-end extend accross the
polygon. I want to use the polylines to essentially cut the polygon into
smaller polygons.  Does anyone have any suggestions where to start with this
problem.  My question may not be completely clear so I have uploaded an
example picture: http://www.flickr.com/photos/42311742@N02/3904105994/

Thanks,

Matt



--
Dr. Marco Hugentobler
HUGIS - GIS programming and consulting
Honrainweg 5
CH-8038 Zürich
[hidden email]
http://homepage.hispeed.ch/hugis/


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

Re: Create Multiple Polygons from Polygon and Polylines

Matthew Denno
Hi Marco,

Thanks for your thoughts.  I will have a look at your suggestions.  It looks like they are definitely the direction that I need to go.

I anticipate I will have some difficulty because, due to the way the line segments are generated, each polyline segment is a separate feature and they are in no particular order.  It looks like maybe the first step I will have to do is create a number of continuous polylines from the segments, then use the methods you (and Carson in an email that missed the list) suggested.  Does that sound correct?  At that point I could determine whether I should use splitFeatures or addRing.

Thanks again for your input.

Matt

On Wed, Sep 9, 2009 at 12:16 PM, Hugentobler Marco <[hidden email]> wrote:
Hi Matthew

For the polylines that cut the polygon, you could use
QgsVectorLayer::splitFeatures (or on feature/geometry level
QgsGeometry::splitGeometry
And for the features that are contained in the polygons, there is
QgsVectorLayer::addRing.

Hope it helps,
Marco


-----Ursprüngliche Nachricht-----
Von: [hidden email] im Auftrag von Matthew Denno
Gesendet: Mi 09.09.2009 16:53
An: qgis-developer
Betreff: [Qgis-developer] Create Multiple Polygons from Polygon and Polylines

Hi All,

I am trying to programmaticly create a polygon shapefile from two separate
input shapefiles.  The first is a boundary polygon shapefile and the second
is a a polyline shapefile that contains a number of segments (could many)
that are spatially contained in the boundary polygon.  The polyline segments
either form closed areas or when connected end-to-end extend accross the
polygon. I want to use the polylines to essentially cut the polygon into
smaller polygons.  Does anyone have any suggestions where to start with this
problem.  My question may not be completely clear so I have uploaded an
example picture: http://www.flickr.com/photos/42311742@N02/3904105994/

Thanks,

Matt



--
Dr. Marco Hugentobler
HUGIS - GIS programming and consulting
Honrainweg 5
CH-8038 Zürich
[hidden email]
http://homepage.hispeed.ch/hugis/




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

Re: Create Multiple Polygons from Polygon and Polylines

motta.luiz@gmail.com
In reply to this post by Matthew Denno
Matt,

I see your picture and my idea are same operations:

1) Split:
Create polygons from layer polygon(source) with polylines(cut).
Polygon data -> Boundary Shapefile
Cut data  -> Polylines segment (only two segments in picture showed) *
these lines cross or touch line boundary of polygon
Result -> three Polygons

2) Make polygon from polyline closed:
Polyline -> Polylines segment (only closed)
Result -> Polygon

3) Union:
Polygon -> polygon from result first item
Polygon -> polygon from result second  item
Result -> All polygon

Strategy:
1) Separate type polylines: Closed (A) and not Closed(B)
2) Make polygon from A
3) Split polygon(big polygon) with B
4) Union result item 2 with result item 3.



Matthew Denno escreveu:

> Hi All,
>
> I am trying to programmaticly create a polygon shapefile from two
> separate input shapefiles.  The first is a boundary polygon shapefile
> and the second is a a polyline shapefile that contains a number of
> segments (could many) that are spatially contained in the boundary
> polygon.  The polyline segments either form closed areas or when
> connected end-to-end extend accross the polygon. I want to use the
> polylines to essentially cut the polygon into smaller polygons.  Does
> anyone have any suggestions where to start with this problem.  My
> question may not be completely clear so I have uploaded an example
> picture: http://www.flickr.com/photos/42311742@N02/3904105994/
>
> Thanks,
>
> Matt
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>  

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

Re: Create Multiple Polygons from Polygon and Polylines

Matthew Denno
Luiz,

Thanks for your thoughts.  I agree with your approach for the most part.  I think that the difficulty for me will be with the implementation and achieving it programmaticlly.

I should have been more specific about the picture that I shared.  When I say segment I am referring to each individual single straight section (i.e. two points) so each of the polylines that you are referring to is actually many individual segments.  That doesn't really change the approach but adds an extra step.  Basically the polylines need to be "dissolved" first.  Is there an existing function to acheive this?  Also, is there an enexisting function to union polygons programaticlly?

Matt



On Mon, Sep 14, 2009 at 6:38 PM, Luiz Motta <[hidden email]> wrote:
Matt,

I see your picture and my idea are same operations:

1) Split:
Create polygons from layer polygon(source) with polylines(cut).
Polygon data -> Boundary Shapefile
Cut data  -> Polylines segment (only two segments in picture showed) * these lines cross or touch line boundary of polygon
Result -> three Polygons

2) Make polygon from polyline closed:
Polyline -> Polylines segment (only closed)
Result -> Polygon

3) Union:
Polygon -> polygon from result first item
Polygon -> polygon from result second  item
Result -> All polygon

Strategy:
1) Separate type polylines: Closed (A) and not Closed(B)
2) Make polygon from A
3) Split polygon(big polygon) with B
4) Union result item 2 with result item 3.



Matthew Denno escreveu:
Hi All,

I am trying to programmaticly create a polygon shapefile from two separate input shapefiles.  The first is a boundary polygon shapefile and the second is a a polyline shapefile that contains a number of segments (could many) that are spatially contained in the boundary polygon.  The polyline segments either form closed areas or when connected end-to-end extend accross the polygon. I want to use the polylines to essentially cut the polygon into smaller polygons.  Does anyone have any suggestions where to start with this problem.  My question may not be completely clear so I have uploaded an example picture: http://www.flickr.com/photos/42311742@N02/3904105994/

Thanks,

Matt
------------------------------------------------------------------------


_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
 



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

Re: Create Multiple Polygons from Polygon and Polylines

Carson Farmer
Basically the polylines need to be "dissolved" first.  Is there an existing function to acheive this?

You will likely need to do this yourself by finding segments that share a node, and creating a single line from all the associated QgsPoints.
 
Also, is there an enexisting function to union polygons programaticlly?

Certainly, check out QgsGeometry.combine() in the docs.

Carson


--
Carson Farmer
National Centre for Geocomputation
John Hume Building,
National University of Ireland, Maynooth,
Maynooth,
Co. Kildare,
Ireland.
www.carsonfarmer.com

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

Re: Create Multiple Polygons from Polygon and Polylines

Matthew Denno
Thank you everyone for your help.  I really appreciate it as I am trying to learn the QGIS API and C++.  I think that I have a solid plan to achieve my goal now.  However, I seem to be stuck on something that I think should be simple, looping through the points of a QgsPolyline.  If I iterate through the Polylines and print out the point coordinates using ->at(int).x() I can print out x1, y1, x2, and y2 with out problems.  But, if I try to iterate through the points of the polyline and print out the coordinates, I get x1, y1 and then a bunch of junk values for the first 3 polylines and then eventually it ends unexpectedly.  Below is the code I am trying to use.

//now iterate through each polyline feature

while (mypPolylineLayer->nextFeature(currentPolylineFeat))

{

qDebug("feature = %i",f);

++f;


//get point geometry for current feature

QgsGeometry * currentPolylineGeom = currentPolylineFeat.geometry();

QgsPolyline * currentPolyline;

currentPolyline = &currentPolylineGeom->asPolyline();

//this part works

qDebug() << "x1 = " << currentPolyline->at(0).x();

qDebug() << "y1 = " << currentPolyline->at(0).y();

qDebug() << "x2 = " << currentPolyline->at(1).x();

qDebug() << "y2 = " << currentPolyline->at(1).y();


QgsPolyline::const_iterator it = currentPolyline->constBegin();

for(; it != currentPolyline->constEnd(); ++it)

{

//this part doesn't

qDebug() << "x = " << (*it).x();

qDebug() << "y = " << (*it).y();

}

}



On Tue, Sep 15, 2009 at 5:48 AM, Carson Farmer <[hidden email]> wrote:
Basically the polylines need to be "dissolved" first.  Is there an existing function to acheive this?

You will likely need to do this yourself by finding segments that share a node, and creating a single line from all the associated QgsPoints.
 
Also, is there an enexisting function to union polygons programaticlly?

Certainly, check out QgsGeometry.combine() in the docs.

Carson


--
Carson Farmer
National Centre for Geocomputation
John Hume Building,
National University of Ireland, Maynooth,
Maynooth,
Co. Kildare,
Ireland.
www.carsonfarmer.com


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

Re: Create Multiple Polygons from Polygon and Polylines

Matthew Denno
Update:  So I thought that QgsPolyline->at(int).x() and QgsPolyline->at(int).y() methods were working but upon further investigation, they aren't working either.  It only returns the correct x value for x1.  All of the other values are very small i.e. 1e-308

On Tue, Sep 15, 2009 at 10:09 PM, Matthew Denno <[hidden email]> wrote:
Thank you everyone for your help.  I really apprecand iate it as I am trying to learn the QGIS API and C++.  I think that I have a solid plan to achieve my goal now.  However, I seem to be stuck on something that I think should be simple, looping through the points of a QgsPolyline.  If I iterate through the Polylines and print out the point coordinates using ->at(int).x() I can print out x1, y1, x2, and y2 with out problems.  But, if I try to iterate through the points of the polyline and print out the coordinates, I get x1, y1 and then a bunch of junk values for the first 3 polylines and then eventually it ends unexpectedly.  Below is the code I am trying to use.

//now iterate through each polyline feature

while (mypPolylineLayer->nextFeature(currentPolylineFeat))

{

qDebug("feature = %i",f);

++f;


//get point geometry for current feature

QgsGeometry * currentPolylineGeom = currentPolylineFeat.geometry();

QgsPolyline * currentPolyline;

currentPolyline = &currentPolylineGeom->asPolyline();

//this part works

qDebug() << "x1 = " << currentPolyline->at(0).x();

qDebug() << "y1 = " << currentPolyline->at(0).y();

qDebug() << "x2 = " << currentPolyline->at(1).x();

qDebug() << "y2 = " << currentPolyline->at(1).y();


QgsPolyline::const_iterator it = currentPolyline->constBegin();

for(; it != currentPolyline->constEnd(); ++it)

{

//this part doesn't

qDebug() << "x = " << (*it).x();

qDebug() << "y = " << (*it).y();

}

}



On Tue, Sep 15, 2009 at 5:48 AM, Carson Farmer <[hidden email]> wrote:
Basically the polylines need to be "dissolved" first.  Is there an existing function to acheive this?

You will likely need to do this yourself by finding segments that share a node, and creating a single line from all the associated QgsPoints.
 
Also, is there an enexisting function to union polygons programaticlly?

Certainly, check out QgsGeometry.combine() in the docs.

Carson


--
Carson Farmer
National Centre for Geocomputation
John Hume Building,
National University of Ireland, Maynooth,
Maynooth,
Co. Kildare,
Ireland.
www.carsonfarmer.com



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

Re: Create Multiple Polygons from Polygon and Polylines

Matthew Denno
Hello All,

So, I have made quite a bit of progress with this function, but I am having a little problem.  It does not work with all datasets. 

For debuging, the program outputs the point coordinates using the toString() function each time it checks for coincident points (when checking if polyline segments touch end to end).  Some points show as being equal using toString() but don't return true when checked using ==.  I suspect that this is due to rounding errors maybe, but that doesn't quite seem right because the source data are equal double values.  Does the toString() function show all the decimal points for the point coordinates?  Is there a better way to check if points are coincident (better than ==)?

I can send out the code if anyone is interested.

Thanks for all your help.

Matt


On Tue, Sep 15, 2009 at 11:10 PM, Matthew Denno <[hidden email]> wrote:
Update:  So I thought that QgsPolyline->at(int).x() and QgsPolyline->at(int).y() methods were working but upon further investigation, they aren't working either.  It only returns the correct x value for x1.  All of the other values are very small i.e. 1e-308

On Tue, Sep 15, 2009 at 10:09 PM, Matthew Denno <[hidden email]> wrote:
Thank you everyone for your help.  I really apprecand iate it as I am trying to learn the QGIS API and C++.  I think that I have a solid plan to achieve my goal now.  However, I seem to be stuck on something that I think should be simple, looping through the points of a QgsPolyline.  If I iterate through the Polylines and print out the point coordinates using ->at(int).x() I can print out x1, y1, x2, and y2 with out problems.  But, if I try to iterate through the points of the polyline and print out the coordinates, I get x1, y1 and then a bunch of junk values for the first 3 polylines and then eventually it ends unexpectedly.  Below is the code I am trying to use.


//now iterate through each polyline feature

while (mypPolylineLayer->nextFeature(currentPolylineFeat))

{

qDebug("feature = %i",f);

++f;


//get point geometry for current feature

QgsGeometry * currentPolylineGeom = currentPolylineFeat.geometry();

QgsPolyline * currentPolyline;

currentPolyline = &currentPolylineGeom->asPolyline();

//this part works

qDebug() << "x1 = " << currentPolyline->at(0).x();

qDebug() << "y1 = " << currentPolyline->at(0).y();

qDebug() << "x2 = " << currentPolyline->at(1).x();

qDebug() << "y2 = " << currentPolyline->at(1).y();


QgsPolyline::const_iterator it = currentPolyline->constBegin();

for(; it != currentPolyline->constEnd(); ++it)

{

//this part doesn't

qDebug() << "x = " << (*it).x();

qDebug() << "y = " << (*it).y();

}

}



On Tue, Sep 15, 2009 at 5:48 AM, Carson Farmer <[hidden email]> wrote:
Basically the polylines need to be "dissolved" first.  Is there an existing function to acheive this?

You will likely need to do this yourself by finding segments that share a node, and creating a single line from all the associated QgsPoints.
 
Also, is there an enexisting function to union polygons programaticlly?

Certainly, check out QgsGeometry.combine() in the docs.

Carson


--
Carson Farmer
National Centre for Geocomputation
John Hume Building,
National University of Ireland, Maynooth,
Maynooth,
Co. Kildare,
Ireland.
www.carsonfarmer.com




_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer