Skip to main content
corrected spelling, improved formatting
Source Link
nmtoken
  • 13.6k
  • 5
  • 38
  • 89

Python - GDAL : Polygon to point shape fileshapefile

I want to read all the points in a polygon shape-fileshapefile and use their coordinates to make a new point shape file.shapefile, then find the min and max of the X coordinates. So here is the code I wrote for the first part (polygon to point) and it makes the point shape fileshapefile but the attribute table is just has the ID field.

from osgeo import gdal
from osgeo import ogr
from osgeo import gdal_array
from osgeo import gdalconst
from osgeo.gdalconst import *
import os
os.chdir('C:\Fcounty')
driver = ogr.GetDriverByName("ESRI Shapefile")
fname = 'PolygonFC.shp'
vector = driver.Open(fname, 0)
layer = vector.GetLayer(0)
ofname = 'PointFC.shp'
if os.path.exists(ofname):
         driver.DeleteDataSource(ofname)

ovector = driver.CreateDataSource(ofname)
olayer = ovector.CreateLayer('points', geom_type=ogr.wkbPoint)
fieldDefn = ogr.FieldDefn('id', ogr.OFTInteger)
olayer.CreateField(fieldDefn)
featureDefn = olayer.GetLayerDefn()
point = ogr.Geometry(ogr.wkbPoint)
k = 0
f = layer.GetNextFeature()
while f:
    geom = f.GetGeometryRef()
    ring = geom.GetGeometryRef(0)
    np = ring.GetPointCount()
    for j in range(np):
        p = ring.GetPoint(j)
        ofeature = ogr.Feature(featureDefn)
        point.AddPoint(p[0], p[1])
        ofeature.SetGeometry(point)
        ofeature.SetField('id', k)
        olayer.CreateFeature(ofeature)
        ofeature.Destroy()
        k += 1
   f.Destroy()
   f = layer.GetNextFeature()
vector.Destroy()
ovector.Destroy()

asAs I mentioned this part is not giving me any error (I know it does not mean it works). So my questions are:

1- is this the right code? why there is no other fields beside 'ID' in attribute table? 2- How can i do the second part of it? (the coordinates for lowest and highest X) I want to get it from the point shape file, not the polygon.

  1. Is this the right code? why there is no other fields beside 'ID' in attribute table?
  2. How can I do the second part of it? (the coordinates for lowest and highest X) I want to get it from the point shapefile, not the polygon.

Python - GDAL : Polygon to point shape file

I want to read all the points in a polygon shape-file and use their coordinates to make a new point shape file. then find the min and max of the X coordinates. So here is the code I wrote for the first part (polygon to point) and it makes the point shape file but the attribute table is just has the ID field.

from osgeo import gdal
from osgeo import ogr
from osgeo import gdal_array
from osgeo import gdalconst
from osgeo.gdalconst import *
import os
os.chdir('C:\Fcounty')
driver = ogr.GetDriverByName("ESRI Shapefile")
fname = 'PolygonFC.shp'
vector = driver.Open(fname, 0)
layer = vector.GetLayer(0)
ofname = 'PointFC.shp'
if os.path.exists(ofname):
         driver.DeleteDataSource(ofname)

ovector = driver.CreateDataSource(ofname)
olayer = ovector.CreateLayer('points', geom_type=ogr.wkbPoint)
fieldDefn = ogr.FieldDefn('id', ogr.OFTInteger)
olayer.CreateField(fieldDefn)
featureDefn = olayer.GetLayerDefn()
point = ogr.Geometry(ogr.wkbPoint)
k = 0
f = layer.GetNextFeature()
while f:
    geom = f.GetGeometryRef()
    ring = geom.GetGeometryRef(0)
    np = ring.GetPointCount()
    for j in range(np):
        p = ring.GetPoint(j)
        ofeature = ogr.Feature(featureDefn)
        point.AddPoint(p[0], p[1])
        ofeature.SetGeometry(point)
        ofeature.SetField('id', k)
        olayer.CreateFeature(ofeature)
        ofeature.Destroy()
        k += 1
   f.Destroy()
   f = layer.GetNextFeature()
vector.Destroy()
ovector.Destroy()

as I mentioned this part is not giving me any error (I know it does not mean it works). So my questions are:

1- is this the right code? why there is no other fields beside 'ID' in attribute table? 2- How can i do the second part of it? (the coordinates for lowest and highest X) I want to get it from the point shape file, not the polygon.

Python - GDAL : Polygon to point shapefile

I want to read all the points in a polygon shapefile and use their coordinates to make a new point shapefile, then find the min and max of the X coordinates. So here is the code I wrote for the first part (polygon to point) and it makes the point shapefile but the attribute table is just has the ID field.

from osgeo import gdal
from osgeo import ogr
from osgeo import gdal_array
from osgeo import gdalconst
from osgeo.gdalconst import *
import os
os.chdir('C:\Fcounty')
driver = ogr.GetDriverByName("ESRI Shapefile")
fname = 'PolygonFC.shp'
vector = driver.Open(fname, 0)
layer = vector.GetLayer(0)
ofname = 'PointFC.shp'
if os.path.exists(ofname):
         driver.DeleteDataSource(ofname)

ovector = driver.CreateDataSource(ofname)
olayer = ovector.CreateLayer('points', geom_type=ogr.wkbPoint)
fieldDefn = ogr.FieldDefn('id', ogr.OFTInteger)
olayer.CreateField(fieldDefn)
featureDefn = olayer.GetLayerDefn()
point = ogr.Geometry(ogr.wkbPoint)
k = 0
f = layer.GetNextFeature()
while f:
    geom = f.GetGeometryRef()
    ring = geom.GetGeometryRef(0)
    np = ring.GetPointCount()
    for j in range(np):
        p = ring.GetPoint(j)
        ofeature = ogr.Feature(featureDefn)
        point.AddPoint(p[0], p[1])
        ofeature.SetGeometry(point)
        ofeature.SetField('id', k)
        olayer.CreateFeature(ofeature)
        ofeature.Destroy()
        k += 1
   f.Destroy()
   f = layer.GetNextFeature()
vector.Destroy()
ovector.Destroy()

As I mentioned this part is not giving me any error (I know it does not mean it works). So my questions are:

  1. Is this the right code? why there is no other fields beside 'ID' in attribute table?
  2. How can I do the second part of it? (the coordinates for lowest and highest X) I want to get it from the point shapefile, not the polygon.
Source Link

Python - GDAL : Polygon to point shape file

I want to read all the points in a polygon shape-file and use their coordinates to make a new point shape file. then find the min and max of the X coordinates. So here is the code I wrote for the first part (polygon to point) and it makes the point shape file but the attribute table is just has the ID field.

from osgeo import gdal
from osgeo import ogr
from osgeo import gdal_array
from osgeo import gdalconst
from osgeo.gdalconst import *
import os
os.chdir('C:\Fcounty')
driver = ogr.GetDriverByName("ESRI Shapefile")
fname = 'PolygonFC.shp'
vector = driver.Open(fname, 0)
layer = vector.GetLayer(0)
ofname = 'PointFC.shp'
if os.path.exists(ofname):
         driver.DeleteDataSource(ofname)

ovector = driver.CreateDataSource(ofname)
olayer = ovector.CreateLayer('points', geom_type=ogr.wkbPoint)
fieldDefn = ogr.FieldDefn('id', ogr.OFTInteger)
olayer.CreateField(fieldDefn)
featureDefn = olayer.GetLayerDefn()
point = ogr.Geometry(ogr.wkbPoint)
k = 0
f = layer.GetNextFeature()
while f:
    geom = f.GetGeometryRef()
    ring = geom.GetGeometryRef(0)
    np = ring.GetPointCount()
    for j in range(np):
        p = ring.GetPoint(j)
        ofeature = ogr.Feature(featureDefn)
        point.AddPoint(p[0], p[1])
        ofeature.SetGeometry(point)
        ofeature.SetField('id', k)
        olayer.CreateFeature(ofeature)
        ofeature.Destroy()
        k += 1
   f.Destroy()
   f = layer.GetNextFeature()
vector.Destroy()
ovector.Destroy()

as I mentioned this part is not giving me any error (I know it does not mean it works). So my questions are:

1- is this the right code? why there is no other fields beside 'ID' in attribute table? 2- How can i do the second part of it? (the coordinates for lowest and highest X) I want to get it from the point shape file, not the polygon.