Hi Everyone<br><br>I'm writting a little script that align points coordinates.<br><br>But I don't know why but I have a problem when my script rebuild data associated to geometries.<br><br>In fact I have a field in REAL who is in my new shapefile well-respected in Real too, but data are writting in integer...<br>
I don't want to use the clone function for data because after this step I want to have access and modify the data too...<br><br>Do you have a solution ?<br><br>Configuration : <br>windows XP<br>Python 2.6<br>GDAL 1.8.0<br>
I try with python 2.7 and GDAL 1.8.1 but the problem is still here.<br><br>Guilhain<br><br>PS : Script and data are attached to this email.<br><br>Script : <br><br>from osgeo import ogr<br>import os<br>## READ<br>driver = ogr.GetDriverByName('ESRI SHAPEFILE')<br>
datasource = driver.Open('src/P_test.shp')<br>layer = datasource.GetLayer(0)<br>layerDefn = layer.GetLayerDefn()<br><br>## INFOS<br>#nb Field<br>nbFieldShp = layerDefn.GetFieldCount()<br>#nb Feat<br>nbFeatShp = layer.GetFeatureCount()<br>
<br># We stock fields Name, fields Type and Fields Width<br>fieldNameShp, fieldTypeShp, fieldWidthShp = [], [], []<br>for i in xrange(nbFieldShp):<br> fieldNameShp.append(layerDefn.GetFieldDefn(i).GetName())<br> fieldTypeShp.append(layerDefn.GetFieldDefn(i).GetType())<br>
fieldWidthShp.append(layerDefn.GetFieldDefn(i).GetWidth())<br> <br>if os.path.isfile('src/P_test2.shp')==True:<br> os.remove('src/P_test2.shp')<br> os.remove('src/P_test2.shx')<br> os.remove('src/P_test2.dbf')<br>
<br>## WRITE<br>newDriver = ogr.GetDriverByName('ESRI SHAPEFILE')<br>newDatasource =newDriver.CreateDataSource('src/P_test2.shp')<br>newLayer =newDatasource.CreateLayer('P_test2', None , 1)<br><br>
# Create Field<br>for i in xrange(nbFieldShp):<br> newField= ogr.FieldDefn(fieldNameShp[i], fieldTypeShp[i])<br> newField.SetWidth(fieldWidthShp[i])<br> newLayer.CreateField(newField)<br> <br><br># Write Features<br>
for i in xrange(nbFeatShp):<br> newFeature = ogr.Feature(newLayer.GetLayerDefn())<br> # Read Shp Feature<br> feature = layer.GetFeature(i)<br> for j in xrange(nbFieldShp):<br> data = feature.GetField(j)<br>
# Write New Feature<br> newFeature.SetField(fieldNameShp[j], data)<br> # Stock Geometry Shp Feature<br> x = feature.GetGeometryRef().GetX()<br> y = feature.GetGeometryRef().GetY()<br> xBase = int((int(x/1)*1))<br>
yBase = int((int(y/1)*1))<br> if x - xBase >= 0.5:<br> x = xBase+0.75<br> else:<br> x = xBase+0.25<br> if y - yBase >= 0.5:<br> y = yBase+0.75<br> else:<br> y = yBase+0.25<br>
#Create Geometry<br> newGeometry = ogr.Geometry(feature.GetGeometryRef().GetGeometryType())<br> newGeometry.AddPoint(x,y)<br> #Write Geometry<br> newFeature.SetGeometry(newGeometry)<br> #The Feat is ready create Feat<br>
newLayer.CreateFeature(newFeature)<br> newFeature.Destroy()<br>newDatasource.Destroy()<br> <br>print 'done !'<br> <br>