Ignore:
Timestamp:
06/01/22 05:01:48 (3 years ago)
Author:
Mathieu Morlighem
Message:

merged trunk-jpl and trunk for revision 27033

Location:
issm/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk

  • issm/trunk/src

  • issm/trunk/src/m/shp/shpread.py

    r25836 r27035  
     1import numpy as np
    12from collections import OrderedDict
    23from os import path
     
    1213    """SHPREAD - read a shapefile and build a list of shapes
    1314
    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
    2021    the last point shall be read (1 to read it, 0 not to).
    2122
     
    2627
    2728    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
    3132        files."
    3233
     
    3738        list = shpread('domainoutline')
    3839
    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
    4243        .shp extension exists.
    4344
     
    4647
    4748    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
    5051    to remain consistent with the pattern established by src/m/exp/expread.py.
    5152
    5253    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
    5455    (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
    5758    src/m/exp/expread.py). Also, modify handling of returned data structure in,
    5859        - src/m/classes/basin.py
     
    7677    Structs = []
    7778    shapes = sf.shapes()
    78     for i in range(len(shapes)):
     79    for i, shape in enumerate(shapes):
    7980        Struct = OrderedDict()
    80         shape = shapes[i]
    8181        if shape.shapeType == shapefile.POINT:
    8282            Struct['x'] = shape.points[0][0]
     
    8888            x = []
    8989            y = []
    90             for j in range(num_points):
    91                 point = shape.points[j]
     90            for j, point in enumerate(shape.points):
    9291                x.append(point[0])
    9392                y.append(point[1])
     
    103102            x = []
    104103            y = []
    105             for j in range(num_points):
    106                 point = shape.points[j]
     104            for j, point in enumerate(shape.points):
    107105                x.append(point[0])
    108106                y.append(point[1])
     
    115113            Struct['Geometry'] = 'Polygon'
    116114        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
    121119            #       malformed.
    122120            #
     
    125123        name = ''
    126124        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"
    128126            fieldname = fields[j][0]
    129127            # '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"
    132130            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'"
    134132        Struct['name'] = name
    135133        Structs.append(Struct)
Note: See TracChangeset for help on using the changeset viewer.