Django

Code

Changeset 6422

Show
Ignore:
Timestamp:
09/24/07 23:25:04 (1 year ago)
Author:
jbronn
Message:

gis: Added the encoding keyword for string fields in OGR data sources; improved docstrings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/utils/layermapping.py

    r6414 r6422  
    1919  lm = LayerMapping(model, source_file, mapping) where, 
    2020 
    21   model -- GeoDjango model (not an instance) 
    22  
    23   data -- OGR-supported data source file (e.g. a shapefile) or 
    24           gdal.DataSource instance 
    25  
    26   mapping -- A python dictionary, keys are strings corresponding 
    27              to the GeoDjango model field, and values correspond to 
    28              string field names for the OGR feature, or if the model field 
    29              is a geographic then it should correspond to the OGR 
    30              geometry type, e.g. 'POINT', 'LINESTRING', 'POLYGON'. 
     21  model: 
     22   GeoDjango model (not an instance) 
     23 
     24  data: 
     25   OGR-supported data source file (e.g. a shapefile) or 
     26    gdal.DataSource instance 
     27 
     28  mapping: 
     29   A python dictionary, keys are strings corresponding 
     30   to the GeoDjango model field, and values correspond to 
     31   string field names for the OGR feature, or if the model field 
     32   is a geographic then it should correspond to the OGR 
     33   geometry type, e.g. 'POINT', 'LINESTRING', 'POLYGON'. 
    3134 
    3235Keyword Args: 
    33   layer:  
    34     The index of the layer to use from the Data Source (defaults to 0) 
     36  layer: 
     37   The index of the layer to use from the Data Source (defaults to 0) 
    3538 
    3639  source_srs: 
    37     Use this to specify the source SRS manually (for example, some  
    38      shapefiles don't come with a '.prj' file).  A SRID integer, a 
    39      WKT string, a SpatialReference, and a SpatialRefSys object are 
    40      all valid parameters here. 
     40   Use this to specify the source SRS manually (for example,  
     41   some shapefiles don't come with a '.prj' file).  An integer SRID, 
     42   a string WKT, and SpatialReference objects are valid parameters. 
     43 
     44  encoding: 
     45   Specifies the encoding of the string in the OGR data source. 
     46   For example, 'latin-1', 'utf-8', and 'cp437' are all valid 
     47   encoding parameters. 
    4148 
    4249Example: 
     
    8592 
    8693 LayerMapping just transformed the three geometries from the SHP file from their 
    87   source spatial reference system (WGS84) to the spatial reference system of 
    88   the GeoDjango model (NAD83).  If no spatial reference system is defined for 
    89   the layer, use the `source_srs` keyword with a SpatialReference object to 
    90   specify one. Further, data is selectively imported from the given data source  
    91   fields into the model fields. 
     94  source spatial reference system (WGS84) to the spatial reference system of 
     95  the GeoDjango model (NAD83).  If no spatial reference system is defined for 
     96  the layer, use the `source_srs` keyword with a SpatialReference object to 
     97  specify one. Further, data is selectively imported from the given data source  
     98  fields into the model fields. 
    9299""" 
    93100from types import StringType, TupleType 
     
    235242    "A class that maps OGR Layers to Django Models." 
    236243 
    237     def __init__(self, model, data, mapping, layer=0, source_srs=None): 
     244    def __init__(self, model, data, mapping, layer=0, source_srs=None, encoding=None): 
    238245        "Takes the Django model, the data source, and the mapping (dictionary)" 
    239246 
     
    257264        self.source_srs = check_srs(self.layer, source_srs) 
    258265 
     266        # Setting the encoding for OFTString fields, if specified. 
     267        if encoding: 
     268            # Making sure the encoding exists, if not a LookupError 
     269            #  exception will be thrown. 
     270            from codecs import lookup 
     271            lookup(encoding) 
     272            self.encoding = encoding 
     273        else: 
     274            self.encoding = None 
     275 
     276    # Either the import will work, or it won't be committed. 
    259277    @transaction.commit_on_success 
    260278    def save(self, verbose=False): 
     
    311329                else: 
    312330                    ## Otherwise, this is an OGR field type 
    313                     fi = feat.index(ogr_field) 
    314                     val = feat[fi].value 
     331                    fld = feat[ogr_field] 
     332 
     333                    if isinstance(fld, OFTString) and self.encoding: 
     334                        # The encoding for OGR data sources may be specified here 
     335                        #  (e.g., 'cp437' for Census Bureau boundary files). 
     336                        val = unicode(fld.value, self.encoding) 
     337                    else: 
     338                        val = fld.value 
    315339 
    316340                if is_fk: 
     
    344368                except Exception, e: 
    345369                    print "Failed to save %s\n  Continuing" % kwargs 
    346