Geoscience Reference
In-Depth Information
args = parser.parse_args()
inputfile = args.inputfile
outputname = args.outputname
output_format = args.output_format
srs = args.srs
coordinates = readXY(inputfile)
createXY(output_format, srs, coordinates, outputname)
The function readXY opens the CSV file assigns it to a list object called
coordinates .
def readXY(inputfile):
coordinates = list (csv.reader(open(inputfile)))
return coordinates
The function createXY is the core part of the program. It defines the OGR
driver and creates the output datasource and layer to store the points. The for loop
iterates through each set of coordinates and extracts the latitude and longitude based
on their positions within the list item. We create a temporary point, MyPoint , which
is an OGR Geometry of type ( ogr.wkbPoint ) 'well known binary point'. We also
create a feature identifier for each point. On successful completion, the programprints
a summary of the number of points added to the spatial layer and its name/format.
def createXY(output_format, srs, coordinates, outputname):
#set the SRS using EPSG code
spatialRef = ogr.osr.SpatialReference()
srs_epsg = spatialRef.ImportFromEPSG(int(srs))
#define output format
if output_format =='Shp':
driver = ogr.GetDriverByName('ESRI Shapefile')
output_ds = driver.CreateDataSource('./' +
str(outputname)
+'.shp')
layer = output_ds.CreateLayer('xylayer',
geom_type=ogr.wkbPoint)
elif output_format =='SQLite':
driver = ogr.GetDriverByName('SQLite')
output_ds = driver.CreateDataSource('./' +
str(outputname) +'.db')
layer = output_ds.CreateLayer('xylayer',
geom_type=ogr.wkbPoint)
else:
print('Vector data format not supported, please use
ESRI Shp or SQLite')
exit 1
 
Search WWH ::




Custom Search