3

I have a function to create buffers from everything in a shapefile, but I'm getting an error on some files: Record's geometry type does not match collection schema's geometry type: 'Polygon' != 'LineString'

Here's my function:

def buffer_file(src, dst, buffer):
  if not os.path.isfile(dst):
    with fiona.open(src) as input:
      input_meta = copy.deepcopy(input.meta)
      input_meta['schema']['properties'] = { 'id': 'int'}

      with fiona.open(dst, 'w', **input_meta) as output:
        for i, point in enumerate(input):
          output.write({
            'properties': { 'id': i },
            'geometry': mapping(shape(point['geometry']).buffer(buffer))
          })

What am I missing to make this work with all files? I works with most files I throw at it flawlessly. I just have some problem files.

1
  • 3
    Add some debug code into your script so that when the error happens you can see a) the input geometry and b) the buffered geometry. That will help with finding the right way to fix the problem.
    – user30184
    Aug 3, 2018 at 7:38

1 Answer 1

3

Try to change destination geometry type into "Polygon". I guess geometry type of your input shapefile is LineString. But buffers are polygons. Briefly, you are trying to add polygons to shapefile whose geometry type is LineString.

def buffer_file(src, dst, buffer):
  if not os.path.isfile(dst):
    with fiona.open(src) as input:
      input_meta = copy.deepcopy(input.meta)
      input_meta['schema']['properties'] = { 'id': 'int'}

      # ADD THIS LINE
      input_meta["schema"]["geometry"] = "Polygon"

      with fiona.open(dst, 'w', **input_meta) as output:
        for i, point in enumerate(input):
          output.write({
            'properties': { 'id': i },
            'geometry': mapping(shape(point['geometry']).buffer(buffer))
          })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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