source: anuga_work/production/shark_bay_2007/convert_shapefiles_to_csv.py @ 4689

Last change on this file since 4689 was 4626, checked in by ole, 17 years ago

work on shark bay

File size: 1.1 KB
Line 
1"""
2
3This uses ogr2ogr to convert shapefiles to gml and then 'manually' to csv
4It is assumed that the shapefile is in UTM coordinates
5"""
6
7import os
8import project
9
10os.chdir(project.polygons_dir)
11for file in os.listdir('.'):
12    root, extension = os.path.splitext(file)
13    if extension == '.shp':
14        s = 'ogr2ogr -f "GML" %s.gml %s.shp' %(root, root)
15        print s
16        os.system(s)
17
18        # Convert gml to csv
19        absolute_root = os.path.join(project.polygons_dir, root)
20       
21        infile = open(absolute_root + '.gml')
22        outfile = open(absolute_root + '.csv', 'w')
23
24        # Search for start of coordinate info
25        gml_code = infile.read()
26        infile.close()
27       
28        i = gml_code.find('<gml:coordinates>')
29        if i >= 0:
30            first = i + 17
31
32        # Search for end of coordinate info
33        i = gml_code.find('</gml:coordinates>')           
34        last = i
35
36        #print dir(outfile)
37        print 'Writing coordinates to', outfile.name
38        coordinates = gml_code[first:last]
39        for line in coordinates.split():
40            outfile.write(line + '\n')
41        outfile.close()
42
43print 'Done'
44       
Note: See TracBrowser for help on using the repository browser.