Line | |
---|
1 | """ |
---|
2 | |
---|
3 | This uses ogr2ogr to convert shapefiles to gml and then 'manually' to csv |
---|
4 | It is assumed that the shapefile is in UTM coordinates |
---|
5 | """ |
---|
6 | |
---|
7 | import os |
---|
8 | import project |
---|
9 | |
---|
10 | os.chdir(project.polygons_dir) |
---|
11 | for 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 | |
---|
43 | print 'Done' |
---|
44 | |
---|
Note: See
TracBrowser
for help on using the repository browser.