1 | from anuga.geospatial_data.geospatial_data import * |
---|
2 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
3 | import project |
---|
4 | from os.path import exists |
---|
5 | from os import sep |
---|
6 | |
---|
7 | def get_zone(filename): |
---|
8 | from anuga.coordinate_transforms.redfearn import redfearn |
---|
9 | fr=open(filename+'.xyz','r') |
---|
10 | line =fr.readline() |
---|
11 | parts = line.rstrip().split() |
---|
12 | zone, easting, northing = redfearn(float(parts[1]),float(parts[0])) |
---|
13 | fr.close |
---|
14 | print easting,northing |
---|
15 | return zone |
---|
16 | |
---|
17 | def clip_csv(filename,poly): |
---|
18 | fr=open(filename+'.csv','r') |
---|
19 | fw=open(filename+'_clipped'+'.csv','w') |
---|
20 | fw.write('x,y,elevation\n') |
---|
21 | #extremes = [East,West,North,South] #May only work for positive values |
---|
22 | extremes=[-1.0e10,1.0e10,-1.0e10,1.0e10] |
---|
23 | for points in poly: |
---|
24 | if points[0]>extremes[0]: |
---|
25 | extremes[0]=points[0] |
---|
26 | elif points[0]<extremes[1]: |
---|
27 | extremes[1]=points[0] |
---|
28 | if points[1]>extremes[2]: |
---|
29 | extremes[2]=points[1] |
---|
30 | elif points[1]<extremes[3]: |
---|
31 | extremes[3]=points[1] |
---|
32 | print "polygon_extent= ", extremes |
---|
33 | |
---|
34 | start = True |
---|
35 | for line in fr: |
---|
36 | if start: |
---|
37 | #Read in Header |
---|
38 | start=False |
---|
39 | else: |
---|
40 | parts = line.rstrip().split(',') |
---|
41 | if float(parts[0])<extremes[0] and float(parts[0])>extremes[1] and float(parts[1])<extremes[2] and float(parts[1])>extremes[3]: |
---|
42 | fw.write(','.join([parts[0],parts[1],parts[2]])+'\n') |
---|
43 | fr.close |
---|
44 | fw.close |
---|
45 | |
---|
46 | def xyz2csv(filename): |
---|
47 | """Convert .xyz file with an arbitrary number of space delimeters to .csv |
---|
48 | format. |
---|
49 | |
---|
50 | File format |
---|
51 | xyz: |
---|
52 | (No Header) |
---|
53 | longitude, latitude, elevation |
---|
54 | |
---|
55 | csv: |
---|
56 | (Header x,y,elevation) |
---|
57 | easting, northing, elevation |
---|
58 | """ |
---|
59 | from anuga.coordinate_transforms.redfearn import redfearn |
---|
60 | |
---|
61 | #Write header |
---|
62 | fr=open(filename+'.xyz','r') |
---|
63 | fw=open(filename+'.csv','w') |
---|
64 | fw.write('x,y,elevation\n') |
---|
65 | for line in fr: |
---|
66 | parts = line.rstrip().split() |
---|
67 | zone, easting, northing = redfearn(float(parts[1]),float(parts[0])) |
---|
68 | fw.write(','.join([str(easting),str(northing),parts[2]])+'\n') |
---|
69 | fr.close |
---|
70 | fw.close |
---|
71 | |
---|
72 | filenames=['patong_bay_1s_grid','andaman_3s_grid'] |
---|
73 | |
---|
74 | dir='data' |
---|
75 | print 'Converting .xyz files to .csv format' |
---|
76 | for filename in filenames: |
---|
77 | if not exists(dir+sep+filename+'.csv'): |
---|
78 | xyz2csv(dir+sep+filename) |
---|
79 | else: |
---|
80 | print dir+sep+filename+'.csv already exists ensure the .xyz changed has not changed' |
---|
81 | |
---|
82 | print 'Clipping .csv files' |
---|
83 | filename='andaman_3s_grid' |
---|
84 | if not exists(dir+sep+filename+'_clipped'+'.csv'): |
---|
85 | clip_csv(dir+sep+filename,project.bounding_polygon) |
---|
86 | else: |
---|
87 | print dir+sep+filename+'_clipped'+'.csv already exists ensure the .csv file has not changed' |
---|
88 | |
---|
89 | print 'Creating geospatial data' |
---|
90 | G0 = Geospatial_data(file_name=dir+sep+filenames[0]+'.csv') |
---|
91 | print 'read bay' |
---|
92 | G1 = Geospatial_data(file_name=dir+sep+filenames[1]+'_clipped.csv') |
---|
93 | print 'read andaman' |
---|
94 | bounding_polygon=ensure_numeric(project.bounding_polygon) |
---|
95 | G1 = G1.clip(bounding_polygon) |
---|
96 | print 'clipped andaman' |
---|
97 | bay_grid_poly=ensure_numeric(project.bay_grid_poly) |
---|
98 | G1 = G1.clip_outside(bay_grid_poly) |
---|
99 | |
---|
100 | # Plot the two point sets |
---|
101 | data_3s=G1.get_data_points() |
---|
102 | data_1s=G0.get_data_points() |
---|
103 | |
---|
104 | from pylab import * |
---|
105 | hold(True) |
---|
106 | plot(data_3s[:,0],data_3s[:,1],'r.') |
---|
107 | #plot(data_1s[:,0],data_1s[:,1],'b.') |
---|
108 | #plot(bounding_polygon[:,0],bounding_polygon[:,1],'k-') |
---|
109 | show() |
---|
110 | |
---|
111 | |
---|
112 | G = G0 + G1 |
---|
113 | |
---|
114 | print 'Creating .pts file' |
---|
115 | G.export_points_file(project.good_combined_dir_name +'.pts') |
---|