Skip to content

Instantly share code, notes, and snippets.

@drnextgis
Created July 13, 2011 12:00
Show Gist options
  • Save drnextgis/1080180 to your computer and use it in GitHub Desktop.
Save drnextgis/1080180 to your computer and use it in GitHub Desktop.
Convert MOD14 csv into ESRI Shapefile
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from optparse import OptionParser
import os, sys, fnmatch
def echo_err(parser,msg):
parser.print_help()
print "*** " + msg
sys.exit(1)
if __name__ == '__main__':
usage = 'usage: %prog --inputdir=PATH --outputdir=PATH'
parser = OptionParser(usage=usage)
parser.add_option('-i', '--inputdir', action='store', dest='inputDir')
parser.add_option('-o', '--outputdir', action='store', dest='outputDir')
(options, args) = parser.parse_args()
inputDir = options.inputDir
outputDir = options.outputDir
if inputDir == None:
inputDir = os.getcwd()
if outputDir == None:
outputDir = os.getcwd()
if not os.path.exists(inputDir):
echo_err(parser,"Input directoty not found")
if not os.path.exists(outputDir):
os.makedirs(outputDir)
for root, dirs, files in os.walk(inputDir):
csvFiles = fnmatch.filter(files, '*.csv')
if (len(csvFiles) != 0):
for fileName in csvFiles:
nf = os.path.join(outputDir,fileName)[:-4] + ".shp"
fvrt = open('work.vrt', 'wt')
fvrt.write('<OGRVRTDataSource>\n'
'<OGRVRTLayer name="' + fileName[:-4] + '">\n'
'<SrcDataSource>' + os.path.join(root,fileName) + '</SrcDataSource>\n'
'<GeometryType>wkbPoint</GeometryType>\n'
'<LayerSRS>WGS84</LayerSRS>\n'
'<GeometryField encoding="PointFromColumns" x="longitude" y="latitude"/>\n'
'</OGRVRTLayer>\n'
'</OGRVRTDataSource>\n')
fvrt.close()
cmd = 'ogr2ogr -overwrite ' + nf + ' work.vrt'
print "converting from ......" + fileName
os.system(cmd)
os.remove("work.vrt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment