Changeset 27035 for issm/trunk/src/m/shp/shpread.py
- Timestamp:
- 06/01/22 05:01:48 (3 years ago)
- Location:
- issm/trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk
- Property svn:mergeinfo changed
/issm/trunk-jpl merged: 26745-26955,26957-27031
- Property svn:mergeinfo changed
-
issm/trunk/src
- Property svn:mergeinfo changed
-
issm/trunk/src/m/shp/shpread.py
r25836 r27035 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)
Note:
See TracChangeset
for help on using the changeset viewer.