Changeset 26969
- Timestamp:
- 04/24/22 08:43:30 (3 years ago)
- Location:
- issm/trunk-jpl/src/m
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/exp/expwrite.py
r26955 r26969 20 20 21 21 fid = open(filename, 'w') 22 for x, y in zip(contours['x'], contours['y']): 23 if len(x) != len(y): 24 raise RuntimeError('contours x and y coordinates must be of identical size') 25 26 if 'name' in contours: 27 fid.write('{}{}\n'.format('## Name:', contours['name'])) 22 #if it is a list we need to loop on several contours 23 if isinstance(contours, list): 24 for contour in contours: 25 #if it si some kind of array it is a contour and we loop on indexes 26 if isinstance(contour['x'], (list, tuple, np.ndarray)): 27 writegeomlist(contour, fid, filename) 28 #else it is an index and we just write it down 29 else: 30 writegeom(contour, fid, filename) 31 #if it is a dict type it means just one contour 32 else: 33 #if it si some kind of array it is a contour and we loop on indexes 34 if isinstance(contours['x'], (list, tuple, np.ndarray)): 35 writegeomlist(contours, fid, filename) 36 #else it is an index and we just write it down 28 37 else: 29 fid.write('{}{}\n'.format('## Name:', filename)) 30 31 fid.write('{}\n'.format('## Icon:0')) 32 fid.write('{}\n'.format('# Points Count Value')) 33 if 'density' in contours: 34 if isinstance(contours['density'], int): 35 fid.write('{} {}\n'.format(np.size(x), contours['density'])) 36 else: 37 fid.write('{} {}\n'.format(np.size(x), 1.)) 38 else: 39 fid.write('{} {}\n'.format(np.size(x), 1.)) 40 fid.write('{}\n'.format('# X pos Y pos')) 41 for xi, yi in zip(x, y): 42 fid.write('%10.10f %10.10f\n' % (xi, yi)) 43 fid.write('\n') 38 writegeom(contours, fid, filename) 44 39 45 40 fid.close() 41 42 43 def writegeomlist(contour, fid, filename): 44 if len(contour['x']) != len(contour['y']): 45 raise RuntimeError('contours x and y coordinates must be of identical size') 46 if 'name' in contour: 47 fid.write('{}{}\n'.format('## Name:', contour['name'])) 48 else: 49 fid.write('{}{}\n'.format('## Name:', filename)) 50 51 fid.write('{}\n'.format('## Icon:0')) 52 fid.write('{}\n'.format('# Points Count Value')) 53 if 'density' in contour: 54 if isinstance(contour['density'], int): 55 fid.write('{} {}\n'.format(np.size(contour['x']), contour['density'])) 56 else: 57 fid.write('{} {}\n'.format(np.size(contour['x']), 1.)) 58 else: 59 fid.write('{} {}\n'.format(np.size(contour['x']), 1.)) 60 fid.write('{}\n'.format('# X pos Y pos')) 61 for x, y in zip(contour['x'], contour['y']): 62 fid.write('%10.10f %10.10f\n' % (x, y)) 63 fid.write('\n') 64 65 66 def writegeom(contour, fid, filename): 67 if 'name' in contour: 68 fid.write('{}{}\n'.format('## Name:', contour['name'])) 69 else: 70 fid.write('{}{}\n'.format('## Name:', filename)) 71 72 fid.write('{}\n'.format('## Icon:0')) 73 fid.write('{}\n'.format('# Points Count Value')) 74 if 'density' in contour: 75 if isinstance(contour['density'], int): 76 fid.write('{} {}\n'.format(1, contour['density'])) 77 else: 78 fid.write('{} {}\n'.format(1, 1.)) 79 else: 80 fid.write('{} {}\n'.format(1, 1.)) 81 fid.write('{}\n'.format('# X pos Y pos')) 82 fid.write('%10.10f %10.10f\n' % (contour['x'], contour['y'])) 83 fid.write('\n') -
issm/trunk-jpl/src/m/shp/shpread.py
r25455 r26969 1 import numpy as np 1 2 from collections import OrderedDict 2 3 from os import path … … 12 13 """SHPREAD - read a shapefile and build a list of shapes 13 14 14 This routine reads a shapefile and builds a list of OrderedDict objects 15 containing the fields x and y corresponding to the coordinates, one for the 16 filename of the shp file, for the density, for the nodes, and a field 17 closed to indicate if the domain is closed. If this initial shapefile is 18 point only, the fields closed and points are ommitted. The first argument 19 is the shapefile to be read and the second argument (optional) indicates if 15 This routine reads a shapefile and builds a list of OrderedDict objects 16 containing the fields x and y corresponding to the coordinates, one for the 17 filename of the shp file, for the density, for the nodes, and a field 18 closed to indicate if the domain is closed. If this initial shapefile is 19 point only, the fields closed and points are ommitted. The first argument 20 is the shapefile to be read and the second argument (optional) indicates if 20 21 the last point shall be read (1 to read it, 0 not to). 21 22 … … 26 27 27 28 Example: 28 From underling PyShp implementation, "The shapefile format is actually 29 a collection of three files. You specify the base filename of the 30 shapefile or the complete filename of any of the shapefile component 29 From underling PyShp implementation, "The shapefile format is actually 30 a collection of three files. You specify the base filename of the 31 shapefile or the complete filename of any of the shapefile component 31 32 files." 32 33 … … 37 38 list = shpread('domainoutline') 38 39 39 "OR any of the other 5+ formats which are potentially part of a 40 shapefile. The library does not care about file extensions". We do, 41 however, check that a file with the base filename or base filename with 40 "OR any of the other 5+ formats which are potentially part of a 41 shapefile. The library does not care about file extensions". We do, 42 however, check that a file with the base filename or base filename with 42 43 .shp extension exists. 43 44 … … 46 47 47 48 NOTE: 48 - OrderedDict objects are used instead of OrderedStruct objects (although 49 addressing in the latter case is closer to the MATLAB struct type) in order 49 - OrderedDict objects are used instead of OrderedStruct objects (although 50 addressing in the latter case is closer to the MATLAB struct type) in order 50 51 to remain consistent with the pattern established by src/m/exp/expread.py. 51 52 52 53 TODO: 53 - Create class that can be used to store and pretty print shape structs 54 - Create class that can be used to store and pretty print shape structs 54 55 (ala OrderedStruct from src/m/qmu/helpers.py). 55 - Convert returned data structure from list of OrderedDict objects to list 56 of OrderedStruct objects and remove corresponding note (see also 56 - Convert returned data structure from list of OrderedDict objects to list 57 of OrderedStruct objects and remove corresponding note (see also 57 58 src/m/exp/expread.py). Also, modify handling of returned data structure in, 58 59 - src/m/classes/basin.py … … 76 77 Structs = [] 77 78 shapes = sf.shapes() 78 for i in range(len(shapes)):79 for i, shape in enumerate(shapes): 79 80 Struct = OrderedDict() 80 shape = shapes[i]81 81 if shape.shapeType == shapefile.POINT: 82 82 Struct['x'] = shape.points[0][0] … … 88 88 x = [] 89 89 y = [] 90 for j in range(num_points): 91 point = shape.points[j] 90 for j, point in enumerate(shape.points): 92 91 x.append(point[0]) 93 92 y.append(point[1]) … … 103 102 x = [] 104 103 y = [] 105 for j in range(num_points): 106 point = shape.points[j] 104 for j, point in enumerate(shape.points): 107 105 x.append(point[0]) 108 106 y.append(point[1]) … … 115 113 Struct['Geometry'] = 'Polygon' 116 114 else: 117 # NOTE: We could do this once before looping over shapes as all 118 # shapes in the file must be of the same type, but we would 119 # need to have a second check anyway in order to know how to 120 # parse the points. So, let's just assume the file is not 115 # NOTE: We could do this once before looping over shapes as all 116 # shapes in the file must be of the same type, but we would 117 # need to have a second check anyway in order to know how to 118 # parse the points. So, let's just assume the file is not 121 119 # malformed. 122 120 # … … 125 123 name = '' 126 124 fields = sf.fields 127 for j in range(1, len(fields)): # skip over first field, which is "DeletionFlag"125 for j in range(1, len(fields)): # skip over first field, which is "DeletionFlag" 128 126 fieldname = fields[j][0] 129 127 # 'id' field gets special treatment 130 if fieldname == 'id':131 name = str(sf.record(i)[j - 1]) # record index is offset by one, again, because of "DeletionFlag"128 if fieldname in ['id', 'fid']: 129 name = str(sf.record(i)[j - 1]) # record index is offset by one, again, because of "DeletionFlag" 132 130 else: 133 setattr(Struct, str(fieldname), sf.record(i)[j - 1]) # cast to string removes "u" from "u'fieldname'"131 setattr(Struct, str(fieldname), sf.record(i)[j - 1]) # cast to string removes "u" from "u'fieldname'" 134 132 Struct['name'] = name 135 133 Structs.append(Struct) -
issm/trunk-jpl/src/m/shp/shpwrite.py
r25163 r26969 23 23 24 24 TODO: 25 - Should we check if there is only one element (see how MATLAB's shaperead 25 - Should we check if there is only one element (see how MATLAB's shaperead 26 26 and shapewrite handle single shapes versus multiple shapes)? 27 27 ''' … … 43 43 44 44 for i in range(len(shp)): 45 sf.field('name', 'C') # TODO: Allow shape ids/names to be passed through45 sf.field('name', 'C') # TODO: Allow shape ids/names to be passed through 46 46 if shapeType == 1: # POINT 47 47 sf.point(shp[i].x, shp[i].y) … … 51 51 points.append([shp[i].x[j], shp[i].y[j]]) 52 52 sf.line(line) 53 elif shapeType == 5: # POLYGON53 elif shapeType == 5: # POLYGON 54 54 points = [] 55 55 for j in range(len(shp[i].x)):
Note:
See TracChangeset
for help on using the changeset viewer.