Geoscience Reference
In-Depth Information
include an option to specify the spatial reference system, in this case defined by the
EPSG code. For the purposes of this example, we will create a CSV file containing
a set of coordinates each defined by an id, latitude, longitude (Fig. 14.5 ):
cat xy.csv
1,3.3,45.5
2,4.5,47.4
3,1.2,47.5
4,2.9,45.3
5,-1.22,48.2
As usual, we start by defining the main function and include the libraries that we
require, including ogr, csv and the argparse . Although we have not included
the metadata specified in the first example, we suggest that it is good practice to
include it in your programs.
#!/usr/bin/env python
try:
from osgeo import ogr, osr
else:
import ogr, osr
import csv
import argparse
We will continue by defining the main function and the inputs to the program
that include the:
Input file name of CSV file;
Output format (in this case either ESRI Shapefile or Spatialite), but it can also be
extended to other formats;
Spatial reference defined by the EPSG code;
Output file name.
After having specified the options using argparse , we call two functions. In
the first instance, readXY is called and we pass the input CSV file, which returns a
list of coordinates. Once this is completed, we call createXY and pass the output
format, EPSG code, list of coordinates and the output file name. These two functions
are included below.
def main():
parser = argparse()
parser.add_argument("-i","--input", help="Input name of
csv file", dest="inputfile", required=True, type="string")
parser.add_argument("-f","--format", help="Output
Format", dest="output_format", type="string")
parser.add_argument("-s","--srs", help="EPSG... Spatial
Reference", dest="srs", type="string")
parser.add_argument("-o","--output", help="Output name
of Spatialite layer", dest="outputname", type="string")
 
 
Search WWH ::




Custom Search