[FWTools] shapefile extent not updated after changing shape geometries

Bryan Keith bryan at geomega.com
Mon Aug 28 12:33:36 EDT 2006


I'm running a short Python script to offset and scale the geometry in a 
line shapefile.  The geometry features move as planned, but the extent 
of the shapefile doesn't update as expected according the ogrinfo.  The 
max xy are changed, but the min xy remain unchanged.  Below I've pasted 
the Python script followed by the ogrinfo output.  Do I need to add 
something to the Python script to get the extents refreshed?  (I think 
this information is the shx file).

Bryan

#mine_to_utm.py
import ogr
#include the .shp extension
#sShpName = "section_lines_test_ln.shp"
sShpName = "test_utm_lnz.shp"
dXOffset = 1711586.62
dYOffset = 14712509.9
dScale = 0.3048
ogrdriver = ogr.GetDriverByName("ESRI Shapefile")
ogrSDS = ogrdriver.Open(sShpName, 1)
mLayer = ogrSDS.GetLayerByName(sShpName[:-4])
for i in range(mLayer.GetFeatureCount()):
   mFeature = mLayer.GetFeature(i)
   mGeom = mFeature.GetGeometryRef()
   #only process simple LINESTRING
   if (mGeom.GetGeometryCount() == 0):
     #create new wkt geometry
     sWkt = "LINESTRING ("
     for j in range(mGeom.GetPointCount()):
       dX = mGeom.GetX(j)
       dY = mGeom.GetY(j)
       dZ = mGeom.GetZ(j)
       dXNew = (dX + dXOffset) * dScale
       dYNew = (dY + dYOffset) * dScale
       sWkt += str(dXNew) + " " + str(dYNew) + " " + str(dZ) + ","
     sWkt = sWkt[:-1] + ")"
     newGeom = ogr.CreateGeometryFromWkt(sWkt)
     mFeature.SetGeometryDirectly(newGeom)
     mLayer.SetFeature(mFeature)
   else:
     print "Geometry not supported."
     print mGeom
     break
ogrSDS.Destroy()

S:\>ogrinfo -al test_utm_lnz.shp
INFO: Open of `test_utm_lnz.shp'
       using driver `ESRI Shapefile' successful.

Layer name: test_utm_lnz
Geometry: 3D Line String
Feature Count: 2
Extent: (25197.816780, 14676.458434) - (30479.027175, 30724.795652)
Layer SRS WKT:
(unknown)
FID_: Integer (9.0)
OGRFeature(test_utm_lnz):0
   FID_ (Integer) = (null)
   LINESTRING (30409.466042881133 30667.436514034867 0,25433.42988651176 
30724.79
5651653782 0)

OGRFeature(test_utm_lnz):1
   FID_ (Integer) = (null)
   LINESTRING (30479.027175426483 14678.760769195855 
0,25197.816779945977 14676.4
58433972672 0)


S:\>mine_to_utm.py

S:\>ogrinfo -al test_utm_lnz.shp
INFO: Open of `test_utm_lnz.shp'
       using driver `ESRI Shapefile' successful.

Layer name: test_utm_lnz
Geometry: 3D Line String
Feature Count: 2
Extent: (25197.816780, 14676.458434) - (530981.609259, 4493737.935230)
Layer SRS WKT:
(unknown)
FID_: Integer (9.0)
OGRFeature(test_utm_lnz):0
   FID_ (Integer) = (null)
   LINESTRING (530960.40702599997 4493720.4521700004 
0,529443.71120500006 4493737
.9352299999 0)

OGRFeature(test_utm_lnz):1
   FID_ (Integer) = (null)
   LINESTRING (530981.60925900005 4488847.1037999997 
0,529371.89633100003 4488846
.4020499997 0)

Bryan Keith
GIS Specialist
Geomega, Inc.
Boulder, CO, USA


More information about the FWTools mailing list