0

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')])}

and I want to return:

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()}

1

I answered this question in another forum. What's going on is this:

  • An output file is opened, but because 'shield_type' is too long a field name for shapefiles, OGR changes it to 'shield_ty'.
  • An error is thrown when you try to write a record with a 'shield_type' property because the field only knows 'shield_ty' (this bug was fixed in Fiona 1.0.4).
  • Thank (or blame) the with expression: it closes the output file regardless of the exception, leaving you with a schemaless file.

The solution is to upgrade to Fiona 1.0.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.