[gdal-dev] Is it possible to add new fields to a shapefile and populate then?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] Is it possible to add new fields to a shapefile and populate then?

Stephen Woodbridge
Hi All,

I have a shapefile(s) and I want to read the features, generate some
metrics about each feature and then add them to the that feature. I'm
using python and one obvious way to to in effect create a new shapefile
with the columns I need to add, then copy the existing shapefile to the
new one adding the additional metrics.

I wondering if I can add the new columns to the shapefile, then do a
read and update.

Pros and cons to these approaches? and how to do the later one if possible?

I looked at the python gdal/ogr cookbook and didn't this specific
example. But I thinking of something along the lines of:

1. open the dataset for read/write
2. layer.CreateField() to add the new fields
3. loop through the features in the layer
4.    and use feature.SetField()
5.    and feature = None to commit the changes

will this work?

Thanks,
   -Steve

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: [gdal-dev] Is it possible to add new fields to a shapefile and populate then?

jratike80
Stephen Woodbridge wrote
Hi All,

I have a shapefile(s) and I want to read the features, generate some
metrics about each feature and then add them to the that feature. I'm
using python and one obvious way to to in effect create a new shapefile
with the columns I need to add, then copy the existing shapefile to the
new one adding the additional metrics.

I wondering if I can add the new columns to the shapefile, then do a
read and update.

Pros and cons to these approaches? and how to do the later one if possible?

I looked at the python gdal/ogr cookbook and didn't this specific
example. But I thinking of something along the lines of:

1. open the dataset for read/write
2. layer.CreateField() to add the new fields
3. loop through the features in the layer
4.    and use feature.SetField()
5.    and feature = None to commit the changes

will this work?

Thanks,
   -Steve
Hi,

You can demonstrate that it is possible with ogrinfo:

ogrinfo  -sql "alter table test add column foo integer" test.shp
ogrinfo  -dialect sqlite -sql "update test set foo=2" test.shp


-Jukka Rahkonen-


Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add new fields to a shapefile and populate then?

Stephen Woodbridge
On 4/3/2017 11:37 PM, jratike80 wrote:

> Stephen Woodbridge wrote
>> Hi All,
>>
>> I have a shapefile(s) and I want to read the features, generate some
>> metrics about each feature and then add them to the that feature. I'm
>> using python and one obvious way to to in effect create a new shapefile
>> with the columns I need to add, then copy the existing shapefile to the
>> new one adding the additional metrics.
>>
>> I wondering if I can add the new columns to the shapefile, then do a
>> read and update.
>>
>> Pros and cons to these approaches? and how to do the later one if
>> possible?
>>
>> I looked at the python gdal/ogr cookbook and didn't this specific
>> example. But I thinking of something along the lines of:
>>
>> 1. open the dataset for read/write
>> 2. layer.CreateField() to add the new fields
>> 3. loop through the features in the layer
>> 4.    and use feature.SetField()
>> 5.    and feature = None to commit the changes
>>
>> will this work?
>>
>> Thanks,
>>    -Steve
>
> Hi,
>
> You can demonstrate that it is possible with ogrinfo:
>
> ogrinfo  -sql "alter table test add column foo integer" test.shp
> ogrinfo  -dialect sqlite -sql "update test set foo=2" test.shp

Hi Jukka,

Ahh, good point, I always seem to forget that fact.
I have gotten the python code to add the new fields, but the data is not
getting written to the file.

     # open the shapefile
     driver = ogr.GetDriverByName('ESRI Shapefile')
     dataSource = driver.Open(infile, 1) # open for rw
     if dataSource is None:
         print "ERROR: could not open '%s' as shapefile!" % (infile)
         sys.exit(1)

     layer = dataSource.GetLayer()
     layer.CreateField(ogr.FieldDefn("area",     ogr.OFTReal))


     for feature in layer:
         geom = feature.GetGeometryRef()
         feature.SetField("area", geom.GetArea())
         feature = None

     dataSource = None

In the cookbook example for "Create a New Shapefile and Add Data" there
is a call to:

layer.CreateFeature(feature)

but I do not see an equivalent call for:

layer.UpdateFeature(feature)

So How do I update the feature and force the data to get written to the
shapefile. I'm presuming the CreateFeature will append a new record
which is not what I want.

-Steve

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add new fields to a shapefile and populate then?

Rutger
Stephen Woodbridge wrote
On 4/3/2017 11:37 PM, jratike80 wrote:
> Stephen Woodbridge wrote

Hi Jukka,

Ahh, good point, I always seem to forget that fact.
I have gotten the python code to add the new fields, but the data is not
getting written to the file.

     # open the shapefile
     driver = ogr.GetDriverByName('ESRI Shapefile')
     dataSource = driver.Open(infile, 1) # open for rw
     if dataSource is None:
         print "ERROR: could not open '%s' as shapefile!" % (infile)
         sys.exit(1)

     layer = dataSource.GetLayer()
     layer.CreateField(ogr.FieldDefn("area",     ogr.OFTReal))


     for feature in layer:
         geom = feature.GetGeometryRef()
         feature.SetField("area", geom.GetArea())
         feature = None

     dataSource = None

In the cookbook example for "Create a New Shapefile and Add Data" there
is a call to:

layer.CreateFeature(feature)

but I do not see an equivalent call for:

layer.UpdateFeature(feature)

So How do I update the feature and force the data to get written to the
shapefile. I'm presuming the CreateFeature will append a new record
which is not what I want.

-Steve

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Hey Stephen,

I think you looking for "layer.SetFeature(feature)", which you can add right before destroying the changed feature object withing your loop.


Regards,
Rutger
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add new fields to a shapefile and populate then?

Stephen Woodbridge
Rutger,

Thanks, that did the trick.

-Steve

On 4/4/2017 6:15 AM, Rutger wrote:

> Stephen Woodbridge wrote
>> On 4/3/2017 11:37 PM, jratike80 wrote:
>>> Stephen Woodbridge wrote
>>
>> Hi Jukka,
>>
>> Ahh, good point, I always seem to forget that fact.
>> I have gotten the python code to add the new fields, but the data is not
>> getting written to the file.
>>
>>      # open the shapefile
>>      driver = ogr.GetDriverByName('ESRI Shapefile')
>>      dataSource = driver.Open(infile, 1) # open for rw
>>      if dataSource is None:
>>          print "ERROR: could not open '%s' as shapefile!" % (infile)
>>          sys.exit(1)
>>
>>      layer = dataSource.GetLayer()
>>      layer.CreateField(ogr.FieldDefn("area",     ogr.OFTReal))
>>
>>
>>      for feature in layer:
>>          geom = feature.GetGeometryRef()
>>          feature.SetField("area", geom.GetArea())
>>          feature = None
>>
>>      dataSource = None
>>
>> In the cookbook example for "Create a New Shapefile and Add Data" there
>> is a call to:
>>
>> layer.CreateFeature(feature)
>>
>> but I do not see an equivalent call for:
>>
>> layer.UpdateFeature(feature)
>>
>> So How do I update the feature and force the data to get written to the
>> shapefile. I'm presuming the CreateFeature will append a new record
>> which is not what I want.
>>
>> -Steve
>>
>> ---
>> This email has been checked for viruses by Avast antivirus software.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> gdal-dev mailing list
>
>> gdal-dev@.osgeo
>
>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>
> Hey Stephen,
>
> I think you looking for "layer.SetFeature(feature)", which you can add right
> before destroying the changed feature object withing your loop.
>
>
> Regards,
> Rutger
>
>
>
>
> --
> View this message in context: http://osgeo-org.1560.x6.nabble.com/gdal-dev-Is-it-possible-to-add-new-fields-to-a-shapefile-and-populate-then-tp5315568p5315653.html
> Sent from the GDAL - Dev mailing list archive at Nabble.com.
> _______________________________________________
> gdal-dev mailing list
> [hidden email]
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev