How to return WKT of entire polygon with shapely?How many interior and exterior rings can a polygon have in a...

Fourth person (in Slavey language)

Are babies of evil humanoid species inherently evil?

Should QA ask requirements to developers?

How strictly should I take "Candidates must be local"?

Word for a person who has no opinion about whether god exists

Is "history" a male-biased word ("his+story")?

Is Gradient Descent central to every optimizer?

infinitive telling the purpose

Is it possible to have an Abelian group under two different binary operations but the binary operations are not distributive?

Reverse string, can I make it faster?

Should I take out a loan for a friend to invest on my behalf?

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

Virginia employer terminated employee and wants signing bonus returned

Do items de-spawn in Diablo?

Why is this plane circling around the Lucknow airport every day?

How much stiffer are 23c tires over 28c?

Single word request: Harming the benefactor

What are some noteworthy "mic-drop" moments in math?

Best approach to update all entries in a list that is paginated?

Placing subfig vertically

Examples of a statistic that is not independent of sample's distribution?

How did the power source of Mar-Vell's aircraft end up with her?



How to return WKT of entire polygon with shapely?


How many interior and exterior rings can a polygon have in a standard ArcGIS shapefile?Shapely unable to tell if polygon contains pointShapely WKT precision/roundingWhat does EMPTY mean in WKT?Decompose the polygon with polyline insideArcpy Create Polygon Geometry or Shapefile/FeatureLayer from WKTPython shapely difference between two polygonsIntersection between an angle and a polygon (postgreSQL + postGIS or Python2.7 + shapely + psycopg2)Dealing with a hole (or inner ring) at the intersection of two MultiPolygon polygons in Python ShapelyOGR multipolygon with holes model













3















Im learning to use shapely and have two shapefiles--forest, and basins. The two shapefiles clearly intersect (basins is completely within forest). But the way I've gone about it only gets the feature at index 0 of each shapefile, which don't intersect. I know where this is taking place in the script, but how do I get the whole polygon shape as wkt instead of just one record? Do I build a list of the wkt geometry one record at a time?



# Basins shapefile to wkt
basins = ogr.Open("data/Basins.shp")
basins_layer = basins.GetLayer(0)
basins_feature = basins_layer.GetFeature(0) # This is the line
# I'm thinking there should probably be a loop here to get the geometry of the whole shapefile
basins_geometry = basins_feature.GetGeometryRef()
basins_wkt = shapely.wkt.loads(basins_geometry.ExportToWkt())


How do I loop through the whole shapefile and build the outer polygon as wkt or is there another function to get the outer boundary of the shapefile? I guess this is called the outer ring. I want wkt of the whole shapefile exterior, rather than the individual polygons that make it up.










share|improve this question

























  • If you're using the ogr module you don't need shapely. All of the functionality of shapely is present in ogr.

    – sgillies
    Jul 24 '17 at 15:09
















3















Im learning to use shapely and have two shapefiles--forest, and basins. The two shapefiles clearly intersect (basins is completely within forest). But the way I've gone about it only gets the feature at index 0 of each shapefile, which don't intersect. I know where this is taking place in the script, but how do I get the whole polygon shape as wkt instead of just one record? Do I build a list of the wkt geometry one record at a time?



# Basins shapefile to wkt
basins = ogr.Open("data/Basins.shp")
basins_layer = basins.GetLayer(0)
basins_feature = basins_layer.GetFeature(0) # This is the line
# I'm thinking there should probably be a loop here to get the geometry of the whole shapefile
basins_geometry = basins_feature.GetGeometryRef()
basins_wkt = shapely.wkt.loads(basins_geometry.ExportToWkt())


How do I loop through the whole shapefile and build the outer polygon as wkt or is there another function to get the outer boundary of the shapefile? I guess this is called the outer ring. I want wkt of the whole shapefile exterior, rather than the individual polygons that make it up.










share|improve this question

























  • If you're using the ogr module you don't need shapely. All of the functionality of shapely is present in ogr.

    – sgillies
    Jul 24 '17 at 15:09














3












3








3


1






Im learning to use shapely and have two shapefiles--forest, and basins. The two shapefiles clearly intersect (basins is completely within forest). But the way I've gone about it only gets the feature at index 0 of each shapefile, which don't intersect. I know where this is taking place in the script, but how do I get the whole polygon shape as wkt instead of just one record? Do I build a list of the wkt geometry one record at a time?



# Basins shapefile to wkt
basins = ogr.Open("data/Basins.shp")
basins_layer = basins.GetLayer(0)
basins_feature = basins_layer.GetFeature(0) # This is the line
# I'm thinking there should probably be a loop here to get the geometry of the whole shapefile
basins_geometry = basins_feature.GetGeometryRef()
basins_wkt = shapely.wkt.loads(basins_geometry.ExportToWkt())


How do I loop through the whole shapefile and build the outer polygon as wkt or is there another function to get the outer boundary of the shapefile? I guess this is called the outer ring. I want wkt of the whole shapefile exterior, rather than the individual polygons that make it up.










share|improve this question
















Im learning to use shapely and have two shapefiles--forest, and basins. The two shapefiles clearly intersect (basins is completely within forest). But the way I've gone about it only gets the feature at index 0 of each shapefile, which don't intersect. I know where this is taking place in the script, but how do I get the whole polygon shape as wkt instead of just one record? Do I build a list of the wkt geometry one record at a time?



# Basins shapefile to wkt
basins = ogr.Open("data/Basins.shp")
basins_layer = basins.GetLayer(0)
basins_feature = basins_layer.GetFeature(0) # This is the line
# I'm thinking there should probably be a loop here to get the geometry of the whole shapefile
basins_geometry = basins_feature.GetGeometryRef()
basins_wkt = shapely.wkt.loads(basins_geometry.ExportToWkt())


How do I loop through the whole shapefile and build the outer polygon as wkt or is there another function to get the outer boundary of the shapefile? I guess this is called the outer ring. I want wkt of the whole shapefile exterior, rather than the individual polygons that make it up.







python shapefile shapely wkt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 23 '17 at 23:57







Alex R

















asked Jul 23 '17 at 21:59









Alex RAlex R

6017




6017













  • If you're using the ogr module you don't need shapely. All of the functionality of shapely is present in ogr.

    – sgillies
    Jul 24 '17 at 15:09



















  • If you're using the ogr module you don't need shapely. All of the functionality of shapely is present in ogr.

    – sgillies
    Jul 24 '17 at 15:09

















If you're using the ogr module you don't need shapely. All of the functionality of shapely is present in ogr.

– sgillies
Jul 24 '17 at 15:09





If you're using the ogr module you don't need shapely. All of the functionality of shapely is present in ogr.

– sgillies
Jul 24 '17 at 15:09










2 Answers
2






active

oldest

votes


















4














To get wkt outer ring of a polygon with shapely, you can use next code with your particular paths:



import fiona
from shapely.geometry import shape, LineString

path = '/home/zeito/pyqgis_data/polygon1.shp' #polygon with only one feature

c = fiona.open(path)

collection = [ shape(item['geometry']) for item in c ]

rings = [ LineString(pol.exterior.coords).wkt for pol in collection ]

print rings[0] #0 because polygon has only one feature (one outer ring)


After running above code at Python Console, for my particular path, I got:




LINESTRING (389535.8208391897 4448641.082016046, 397951.9055779715
4459595.351041127, 418925.3231016023 4456522.812168239, 425070.4008473795 4441160.117803795, 412513.0680625305 4427801.253139063, 392341.1824187837 4435148.628704665, 389535.8208391897 4448641.082016046)




By using QuickWKT plugin of QGIS, it can be observed that it works as expected:



enter image description here



Editing Note:



If you have issues to install fiona, an alternative code, by using ogr python module, is the following:



from osgeo import ogr
from shapely.wkt import loads
from shapely.geometry import LineString

path = '/home/zeito/pyqgis_data/polygon1.shp'

basins = ogr.Open(path)

layer = basins.GetLayer()

geoms = []

for feature in layer:
geom = feature.GetGeometryRef()
geoms.append(geom.ExportToWkt())

pol = loads(geoms[0])

print LineString(pol.exterior.coords).wkt


It produces same result that first code.






share|improve this answer


























  • This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

    – sgillies
    Jul 24 '17 at 15:13











  • Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

    – Alex R
    Jul 24 '17 at 18:56











  • As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

    – xunilk
    Jul 24 '17 at 22:42



















0














You can just use .wkt as wkt_polygon = ShapelyPolygon(..).wkt





share























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "79"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f249657%2fhow-to-return-wkt-of-entire-polygon-with-shapely%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    To get wkt outer ring of a polygon with shapely, you can use next code with your particular paths:



    import fiona
    from shapely.geometry import shape, LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp' #polygon with only one feature

    c = fiona.open(path)

    collection = [ shape(item['geometry']) for item in c ]

    rings = [ LineString(pol.exterior.coords).wkt for pol in collection ]

    print rings[0] #0 because polygon has only one feature (one outer ring)


    After running above code at Python Console, for my particular path, I got:




    LINESTRING (389535.8208391897 4448641.082016046, 397951.9055779715
    4459595.351041127, 418925.3231016023 4456522.812168239, 425070.4008473795 4441160.117803795, 412513.0680625305 4427801.253139063, 392341.1824187837 4435148.628704665, 389535.8208391897 4448641.082016046)




    By using QuickWKT plugin of QGIS, it can be observed that it works as expected:



    enter image description here



    Editing Note:



    If you have issues to install fiona, an alternative code, by using ogr python module, is the following:



    from osgeo import ogr
    from shapely.wkt import loads
    from shapely.geometry import LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp'

    basins = ogr.Open(path)

    layer = basins.GetLayer()

    geoms = []

    for feature in layer:
    geom = feature.GetGeometryRef()
    geoms.append(geom.ExportToWkt())

    pol = loads(geoms[0])

    print LineString(pol.exterior.coords).wkt


    It produces same result that first code.






    share|improve this answer


























    • This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

      – sgillies
      Jul 24 '17 at 15:13











    • Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

      – Alex R
      Jul 24 '17 at 18:56











    • As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

      – xunilk
      Jul 24 '17 at 22:42
















    4














    To get wkt outer ring of a polygon with shapely, you can use next code with your particular paths:



    import fiona
    from shapely.geometry import shape, LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp' #polygon with only one feature

    c = fiona.open(path)

    collection = [ shape(item['geometry']) for item in c ]

    rings = [ LineString(pol.exterior.coords).wkt for pol in collection ]

    print rings[0] #0 because polygon has only one feature (one outer ring)


    After running above code at Python Console, for my particular path, I got:




    LINESTRING (389535.8208391897 4448641.082016046, 397951.9055779715
    4459595.351041127, 418925.3231016023 4456522.812168239, 425070.4008473795 4441160.117803795, 412513.0680625305 4427801.253139063, 392341.1824187837 4435148.628704665, 389535.8208391897 4448641.082016046)




    By using QuickWKT plugin of QGIS, it can be observed that it works as expected:



    enter image description here



    Editing Note:



    If you have issues to install fiona, an alternative code, by using ogr python module, is the following:



    from osgeo import ogr
    from shapely.wkt import loads
    from shapely.geometry import LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp'

    basins = ogr.Open(path)

    layer = basins.GetLayer()

    geoms = []

    for feature in layer:
    geom = feature.GetGeometryRef()
    geoms.append(geom.ExportToWkt())

    pol = loads(geoms[0])

    print LineString(pol.exterior.coords).wkt


    It produces same result that first code.






    share|improve this answer


























    • This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

      – sgillies
      Jul 24 '17 at 15:13











    • Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

      – Alex R
      Jul 24 '17 at 18:56











    • As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

      – xunilk
      Jul 24 '17 at 22:42














    4












    4








    4







    To get wkt outer ring of a polygon with shapely, you can use next code with your particular paths:



    import fiona
    from shapely.geometry import shape, LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp' #polygon with only one feature

    c = fiona.open(path)

    collection = [ shape(item['geometry']) for item in c ]

    rings = [ LineString(pol.exterior.coords).wkt for pol in collection ]

    print rings[0] #0 because polygon has only one feature (one outer ring)


    After running above code at Python Console, for my particular path, I got:




    LINESTRING (389535.8208391897 4448641.082016046, 397951.9055779715
    4459595.351041127, 418925.3231016023 4456522.812168239, 425070.4008473795 4441160.117803795, 412513.0680625305 4427801.253139063, 392341.1824187837 4435148.628704665, 389535.8208391897 4448641.082016046)




    By using QuickWKT plugin of QGIS, it can be observed that it works as expected:



    enter image description here



    Editing Note:



    If you have issues to install fiona, an alternative code, by using ogr python module, is the following:



    from osgeo import ogr
    from shapely.wkt import loads
    from shapely.geometry import LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp'

    basins = ogr.Open(path)

    layer = basins.GetLayer()

    geoms = []

    for feature in layer:
    geom = feature.GetGeometryRef()
    geoms.append(geom.ExportToWkt())

    pol = loads(geoms[0])

    print LineString(pol.exterior.coords).wkt


    It produces same result that first code.






    share|improve this answer















    To get wkt outer ring of a polygon with shapely, you can use next code with your particular paths:



    import fiona
    from shapely.geometry import shape, LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp' #polygon with only one feature

    c = fiona.open(path)

    collection = [ shape(item['geometry']) for item in c ]

    rings = [ LineString(pol.exterior.coords).wkt for pol in collection ]

    print rings[0] #0 because polygon has only one feature (one outer ring)


    After running above code at Python Console, for my particular path, I got:




    LINESTRING (389535.8208391897 4448641.082016046, 397951.9055779715
    4459595.351041127, 418925.3231016023 4456522.812168239, 425070.4008473795 4441160.117803795, 412513.0680625305 4427801.253139063, 392341.1824187837 4435148.628704665, 389535.8208391897 4448641.082016046)




    By using QuickWKT plugin of QGIS, it can be observed that it works as expected:



    enter image description here



    Editing Note:



    If you have issues to install fiona, an alternative code, by using ogr python module, is the following:



    from osgeo import ogr
    from shapely.wkt import loads
    from shapely.geometry import LineString

    path = '/home/zeito/pyqgis_data/polygon1.shp'

    basins = ogr.Open(path)

    layer = basins.GetLayer()

    geoms = []

    for feature in layer:
    geom = feature.GetGeometryRef()
    geoms.append(geom.ExportToWkt())

    pol = loads(geoms[0])

    print LineString(pol.exterior.coords).wkt


    It produces same result that first code.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 24 '17 at 22:39

























    answered Jul 24 '17 at 0:58









    xunilkxunilk

    14.8k31842




    14.8k31842













    • This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

      – sgillies
      Jul 24 '17 at 15:13











    • Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

      – Alex R
      Jul 24 '17 at 18:56











    • As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

      – xunilk
      Jul 24 '17 at 22:42



















    • This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

      – sgillies
      Jul 24 '17 at 15:13











    • Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

      – Alex R
      Jul 24 '17 at 18:56











    • As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

      – xunilk
      Jul 24 '17 at 22:42

















    This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

    – sgillies
    Jul 24 '17 at 15:13





    This is very close to what I suggest: iterate over all records in the shapefile as above, get the individual polygon geometries, and make a multipolygon geometry from them. You can then compare this multipolygon to other geometries or dissolve it into a single polygon using the unary_union function.

    – sgillies
    Jul 24 '17 at 15:13













    Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

    – Alex R
    Jul 24 '17 at 18:56





    Thanks. This looks like a good solution and I've been trying all day to get Fiona to no avail. I've been able to successfully install it using pip, but can't import it in python.

    – Alex R
    Jul 24 '17 at 18:56













    As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

    – xunilk
    Jul 24 '17 at 22:42





    As you have issues to install fiona, I modified my first code to obtain same result (see my Editing Note) but, by using org python module.

    – xunilk
    Jul 24 '17 at 22:42













    0














    You can just use .wkt as wkt_polygon = ShapelyPolygon(..).wkt





    share




























      0














      You can just use .wkt as wkt_polygon = ShapelyPolygon(..).wkt





      share


























        0












        0








        0







        You can just use .wkt as wkt_polygon = ShapelyPolygon(..).wkt





        share













        You can just use .wkt as wkt_polygon = ShapelyPolygon(..).wkt






        share











        share


        share










        answered 1 min ago









        AtihskaAtihska

        1246




        1246






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f249657%2fhow-to-return-wkt-of-entire-polygon-with-shapely%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            Энтрерриос (город) Содержание История | Географическое...