[Gdal-dev] OGR and python: File creation question...

Aykut KOÇAK aykutkocak at superonline.com
Tue Sep 26 14:25:10 EDT 2006


Thank you for answer, working for me as well.

Bryan Keith wrote:

> Merhaba Aykut,
>
> See below.
>
> Aykut KOÇAK wrote:
>
>> Hi to all,
>>
>> I am trying the following simple python code to create a ESRI Shape 
>> file. This script seems working fine. But when I checked the new file 
>> with ogrinfo, I can not see the field I am trying to have within SHP 
>> file. What am i doing wrong with this? Thanks in advance for all 
>> suggestions.
>>
>>
>> Regards,
>>
>>
>> Python Code:
>> --------------------
>>
>> #
>> import sys,os
>> # Vector Mapping support
>> import ogr
>>
>> if __name__=="__main__":
>>     # Create the driver to use
>>     drv=ogr.GetDriverByName("ESRI Shapefile")
>>     # Create the DataSource to be written
>>     ds=drv.CreateDataSource("test.shp")
>>     # Create a layer
>>     lyr=ds.CreateLayer(name='test_layer',geom_type=ogr.wkbPoint)
>>     # Prepare the structure of the a feature.
>>     field_def=ogr.FieldDefn('test1',ogr.OFTInteger)
>
>
> At this point in my code that works I have a line like this:
>
> lyr.CreateField(field_def)
>
> Hope that helps.
>
> Bryan
>
>>     feature_def=ogr.FeatureDefn(name='test_feature')
>>     feature_def.AddFieldDefn(field_def)
>>     feature_def.SetGeomType(ogr.wkbPoint)
>>     # Create a feature using prepared structure
>>     feature=ogr.Feature(feature_def)
>>     # Set the value of the field for this feature
>>     feature.SetField(0,173)
>>     # Set the Geometry for this feature
>>     geom=ogr.CreateGeometryFromWkt('POINT(%f %f)' % (32.0,39.0))
>>     feature.SetGeometryDirectly(geom)
>>     # Add this new feature to the Layer
>>     lyr.CreateFeature(feature)
>>     # be sure everything written to disk
>>     lyr.SyncToDisk()
>>     # Destroy the Data Source
>>     ds.Destroy()
>>     # exit gracefully
>>     sys.exit(0)
>>   
>>
>> ogrinfo results:
>> ---------------------------------------------
>> oot at clown-aero:~/project/python# ogrinfo test.shp -al
>> INFO: Open of `test.shp'
>> using driver `ESRI Shapefile' successful.
>>
>> Layer name: test
>> Geometry: Point
>> Feature Count: 1
>> Extent: (32.000000, 39.000000) - (32.000000, 39.000000)
>> Layer SRS WKT:
>> (unknown)
>> FID: Real (11.0)
>> OGRFeature(test):0
>>   FID (Real) =           0
>>   POINT (32 39)
>>
>>
>>
>
>
>


-- 
Aykut KOÇAK

 Elektronik Mühendisi
(/Electronics Engineer/)
 
 E-Posta
(/E-Mail/ )

 aykutkocak at superonline.com




More information about the Gdal-dev mailing list