Created
April 21, 2016 16:22
-
-
Save snorfalorpagus/2285d33e528f2548dda05b376cafc0a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This example converts all of the MapInfo layers in a folder into ESRI Shapefiles. | |
This is complicated a little by the fact that MapInfo geometries are not | |
homogenous, i.e. you can have different kinds of geometry (LineString, Polygon, ...) | |
in the same file. This approach assumes that all of the geometries have the same | |
type as the first feature in the layer. | |
""" | |
import os | |
import fiona | |
import copy | |
folder = r"D:\tmp" | |
# get list of files to process | |
filenames = os.listdir(folder) | |
filenames = [filename for filename in filenames if filename.lower().endswith('.mif')] | |
# this could be the same as the input folder... | |
output_folder = r"D:\tmp" | |
for filename in filenames: | |
path_mapinfo = os.path.join(folder, filename) | |
path_esri = os.path.join(output_folder, '.'.join(filename.split('.')[:-1]) + '.shp') | |
print(filename) | |
# open existing file for reading | |
with fiona.open(path_mapinfo, 'r') as src: | |
# sniff geometry type | |
feature = next(src) | |
geometry_type = feature['geometry']['type'] | |
# change driver, but leave everything else untouched | |
meta = copy.deepcopy(src.meta) | |
meta['driver'] = "ESRI Shapefile" | |
meta['schema']['geometry'] = geometry_type | |
with fiona.open(path_esri, 'w', **meta) as dst: | |
for feature in src: | |
assert(feature['geometry']['type'] == geometry_type) | |
dst.write(feature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment