Discussion:
[Community] Trying to add fields using meta
Ari Simmons
2014-05-28 22:04:06 UTC
Permalink
I am trying to copy the schema of an existing shapefile and add to it in an
output shapefile. Somehow I am not getting anywhere with this...though the
docs seem very clear (and the download page :
https://pypi.python.org/pypi/Fiona) and it is just adding to a dictionary.

For this object, I have this schema
c =
fiona.open(r'I:\It_24\115507_Road_Shields_Label_Processing\data\test_data.shp')
c.schema
{'geometry': 'LineString', 'properties': OrderedDict([(u'ID', 'float:11'),
(u'OSM_ID', 'float:19'), (u'NAME', 'str:254'), (u'TYPE', 'str:254'),
(u'TUNNEL', 'int:6'), (u'BRIDGE', 'int:6'), (u'ONEWAY', 'int:6'), (u'REF',
'str:254'), (u'Z_ORDER', 'int:6')])}
c =
fiona.open(r'I:\It_24\115507_Road_Shields_Label_Processing\data\test_data_copy.shp')
c.schema
{'geometry': 'LineString', 'properties': OrderedDict([(u'ID', 'float:11'),
(u'OSM_ID', 'float:19'), (u'NAME', 'str:254'), (u'TYPE', 'str:254'),
(u'TUNNEL', 'int:6'), (u'BRIDGE', 'int:6'), (u'ONEWAY', 'int:6'), (u'REF',
'str:254'), (u'Z_ORDER', 'int:6'), (u'shield_type', 'str:254'), (u'label',
'str:254'), (u'label_len', 'int:10'), (u'zoom', 'int:10')])}

to do so I am running:

def process_file(self, inFile, outFile):
with fiona.open(inFile, 'r') as input:
meta = input.meta
# create new fields for the new schema
meta['schema']['properties']['shield_type'.encode("utf-8")] =
'str:254'
meta['schema']['properties']['label'.encode("utf-8")] =
'str:254'
meta['schema']['properties']['label_len'] = 'int:10'
meta['schema']['properties']['zoom'] = 'int:10'

with fiona.open(outFile, 'w', **meta) as output:
for item in input:
n = item.copy()
new_data_attributes =
function_using_some_regex_parsing(item['properties']['REF']
...

Right now running this I just get an empty dictionary...I'm not sure what
I'm missing...
c =
fiona.open(r'I:\It_24\115507_Road_Shields_Label_Processing\data\test_data_copy.shp')
c.schema
{'geometry': 'LineString', 'properties': OrderedDict()}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140528/2788d117/attachment.htm>
Sean Gillies
2014-05-29 04:29:39 UTC
Permalink
Hi Ari,

Is there any chance that the
I:\It_24\115507_Road_Shields_Label_Processing\data\test_data_copy.shp isn't
being written properly due to some logic in your function? My code below
writes an (empty) shapefile with the requested fields:

$ python
Python 2.7.6 (default, May 8 2014, 07:38:16)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Post by Ari Simmons
import fiona
from collections import OrderedDict
schema = {'geometry': 'LineString', 'properties': OrderedDict([(u'ID',
'float:11'), (u'OSM_ID', 'float:19'), (u'NAME', 'str:254'), (u'TYPE',
'str:254'), (u'TUNNEL', 'int:6'), (u'BRIDGE', 'int:6'), (u'ONEWAY',
'int:6'), (u'REF', 'str:254'), (u'Z_ORDER', 'int:6'), (u'shield_type',
'str:254'), (u'label', 'str:254'), (u'label_len', 'int:10'), (u'zoom',
'int:10')])}
Post by Ari Simmons
with fiona.open('/tmp/foo.shp', 'w', crs={'init': 'epsg:4326'},
schema=schema, driver='ESRI Shapefile') as output:
... pass
...
Post by Ari Simmons
import subprocess
subprocess.check_output(['ogrinfo', '/tmp/foo.shp', 'foo'])
'INFO: Open of `/tmp/foo.shp\'\n using driver `ESRI Shapefile\'
successful.\n\nLayer name: foo\nGeometry: Line String\nFeature Count:
0\nExtent: (0.000000, 0.000000) - (0.000000, 0.000000)\nLayer SRS
WKT:\nGEOGCS["GCS_WGS_1984",\n DATUM["WGS_1984",\n
SPHEROID["WGS_84",6378137,298.257223563]],\n PRIMEM["Greenwich",0],\n
UNIT["Degree",0.017453292519943295]]\nID: Real (11.0)\nOSM_ID: Real
(19.0)\nNAME: String (254.0)\nTYPE: String (254.0)\nTUNNEL: Integer
(6.0)\nBRIDGE: Integer (6.0)\nONEWAY: Integer (6.0)\nREF: String
(254.0)\nZ_ORDER: Integer (6.0)\nshield_typ: String (254.0)\nlabel: String
(254.0)\nlabel_len: Integer (10.0)\nzoom: Integer (10.0)\n'
Post by Ari Simmons
I am trying to copy the schema of an existing shapefile and add to it in
an output shapefile. Somehow I am not getting anywhere with this...though
https://pypi.python.org/pypi/Fiona) and it is just adding to a dictionary.
For this object, I have this schema
c =
fiona.open(r'I:\It_24\115507_Road_Shields_Label_Processing\data\test_data.shp')
c.schema
{'geometry': 'LineString', 'properties': OrderedDict([(u'ID', 'float:11'),
(u'OSM_ID', 'float:19'), (u'NAME', 'str:254'), (u'TYPE', 'str:254'),
(u'TUNNEL', 'int:6'), (u'BRIDGE', 'int:6'), (u'ONEWAY', 'int:6'), (u'REF',
'str:254'), (u'Z_ORDER', 'int:6')])}
c =
fiona.open(r'I:\It_24\115507_Road_Shields_Label_Processing\data\test_data_copy.shp')
c.schema
{'geometry': 'LineString', 'properties': OrderedDict([(u'ID', 'float:11'),
(u'OSM_ID', 'float:19'), (u'NAME', 'str:254'), (u'TYPE', 'str:254'),
(u'TUNNEL', 'int:6'), (u'BRIDGE', 'int:6'), (u'ONEWAY', 'int:6'), (u'REF',
'str:254'), (u'Z_ORDER', 'int:6'), (u'shield_type', 'str:254'), (u'label',
'str:254'), (u'label_len', 'int:10'), (u'zoom', 'int:10')])}
meta = input.meta
# create new fields for the new schema
meta['schema']['properties']['shield_type'.encode("utf-8")] =
'str:254'
meta['schema']['properties']['label'.encode("utf-8")] =
'str:254'
meta['schema']['properties']['label_len'] = 'int:10'
meta['schema']['properties']['zoom'] = 'int:10'
n = item.copy()
new_data_attributes =
function_using_some_regex_parsing(item['properties']['REF']
...
Right now running this I just get an empty dictionary...I'm not sure what
I'm missing...
c =
fiona.open(r'I:\It_24\115507_Road_Shields_Label_Processing\data\test_data_copy.shp')
c.schema
{'geometry': 'LineString', 'properties': OrderedDict()}
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140528/8908242a/attachment.htm>
Loading...