Django

Code

Changeset 6423

Show
Ignore:
Timestamp:
09/25/07 01:14:01 (1 year ago)
Author:
jbronn
Message:

gis: renamed inspect_data and sample() to ogrinfo, and now displays the extent and field type information.

Files:

Legend:

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

    r6414 r6423  
    33""" 
    44 
    5 from django.contrib.gis.utils.inspect_data import sample 
    6  
    7 # Importing LayerMapping (will not be done if GDAL is not installed) 
     5# Importing LayerMapping and ogrinfo (will not be done if GDAL is not 
     6#  installed) 
    87from django.contrib.gis.gdal import HAS_GDAL 
    98if HAS_GDAL: 
     9    from django.contrib.gis.utils.ogrinfo import ogrinfo, sample 
    1010    from django.contrib.gis.utils.layermapping import LayerMapping 
    1111 
  • django/branches/gis/django/contrib/gis/utils/ogrinfo.py

    r5755 r6423  
    11""" 
    22This module includes some utility functions for inspecting the layout 
    3 of a gdal.DataSource. 
     3of a GDAL data source -- the functionality is analogous to the output 
     4produced by the `ogrinfo` utility. 
    45""" 
    56 
     7from django.contrib.gis.gdal import DataSource 
    68from django.contrib.gis.gdal.geometries import GEO_CLASSES 
    79 
    8 def sample(data_source, num_features=10, gcs_file=None): 
     10def ogrinfo(data_source, num_features=10): 
    911    """ 
    10     Walks the available layers in the supplied ``data_source``, displaying 
    11     the fields for the first ``num_features`` features. 
     12    Walks the available layers in the supplied `data_source`, displaying 
     13    the fields for the first `num_features` features. 
    1214    """ 
     15 
     16    # Checking the parameters. 
     17    if isinstance(data_source, str): 
     18        data_source = DataSource(data_source) 
     19    elif isinstance(data_source, DataSource): 
     20        pass 
     21    else: 
     22        raise Exception, 'Data source parameter must be a string or a DataSource object.' 
    1323 
    1424    for i, layer in enumerate(data_source): 
     
    1828        print "  # features: %s" % len(layer) 
    1929        print "         srs: %s" % layer.srs 
    20         print "Showing first %s features ========" % num_features 
     30        extent_tup = layer.extent.tuple 
     31        print "      extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4]) 
     32        print "Displaying the first %s features ====" % num_features 
    2133 
    2234        width = max(*map(len,layer.fields)) 
    23         fmt = " %%%ss:%%s" % width 
     35        fmt = " %%%ss: %%s" % width 
    2436        for i, feature in enumerate(layer[:num_features]): 
    25             print "======== Feature %s" % i 
     37            print "=== Feature %s" % i 
    2638            for field in layer.fields: 
    27                 print fmt % (field, feature.get(field)) 
     39                fld_typ = feature[field].__class__.__name__.replace('OFT', '') 
     40                output = fmt % (field, fld_typ) 
     41                val = feature.get(field) 
     42                if val: 
     43                    if isinstance(val, str): 
     44                        val_fmt = ' ("%s")' 
     45                    else: 
     46                        val_fmt = ' (%s)' 
     47                    output += val_fmt % val 
     48                else: 
     49                    output += ' (None)' 
     50                print output 
     51 
     52# For backwards compatibility. 
     53sample = ogrinfo