Changes between Version 33 and Version 34 of BluePrint/GIS/ShapefileLayers


Ignore:
Timestamp:
05/17/13 18:50:57 (8 years ago)
Author:
Fran Boon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • BluePrint/GIS/ShapefileLayers

    v33 v34  
    4343
    4444=== Import into native Tables ===
     45SHP should be converted to an lxml.etree for import using XSLT.
    4546
    46 See below for code to convert SHP to an lxml.etree for import using XSLT
    47 
    48 Paul Porthouse [2013-04-27 at the [http://mapaction.org MapAction] Hackathon]:
    49 
    50 Our idea is to use a basic web form with a file upload (similar to the Import from !OpenStreetMap) where you can select which Shapefile to use. You should also be able to set basic information including database connection details. This will then upload the shape and use OGR2OGR to convert the Shapefile to geometry and import it into the specified database.
    51 
    52 A basic OGR2OGR which should accomplish the import is:
    53 {{{OGR2OGR -overwrite -f "PostgreSQL" PG:dbname=databaseconnection shapefile.sh}}}
    54 
    55 This could be called directly from Python by running it as a sub process.
    56 
    57 Alternatively, to use Python bindings, you could perform the import using GDAL which would allow more control over the import directly within Python, but should still handle the import into Postgres.
    58 
     47Working sample script below has been started to be imported into the right place in Sahana here:
     48* https://github.com/flavour/eden/blob/master/modules/s3/codecs/shp.py#L228
    5949
    6050{{{
     
    7464    print "Usage: importshape.py filename"
    7565    sys.exit(0)
    76    
     66
    7767shapefilename = sys.argv[1]
    7868
     
    8070
    8171# Create the datasource
    82 ds = ogr.Open( shapefilename )
     72ds = ogr.Open(shapefilename)
    8373
    8474# Open the shapefile
     
    8676    print "Open failed.\n"
    8777    sys.exit(0)
    88    
     78
    8979# Get the layer and iterate through the features
    9080lyr = ds.GetLayer(0)
    9181
    92 root = etree.Element("shapefile",name=layername)
     82root = etree.Element("shapefile", name=layername)
    9383
    9484for feat in lyr:
    95        
    96     featurenode = etree.SubElement(root,"feature")
    97    
     85
     86    featurenode = etree.SubElement(root, "feature")
     87
    9888    feat_defn = lyr.GetLayerDefn()
    99                
     89
    10090    for i in range(feat_defn.GetFieldCount()):
    101                
     91
    10292        field_defn = feat_defn.GetFieldDefn(i)
    103                        
    104         fieldnode = etree.SubElement(featurenode,field_defn.GetName())
    105                
     93
     94        fieldnode = etree.SubElement(featurenode, field_defn.GetName())
     95
    10696        if field_defn.GetType() == ogr.OFTInteger:
    10797            fieldnode.text = str(feat.GetFieldAsInteger(i))
     
    112102        elif field_defn.GetType() == ogr.OFTString:
    113103            FieldString = str(feat.GetFieldAsString(i))
    114             fieldnode.text = FieldString.decode(encoding='UTF-8',errors='strict')
    115            
    116     wktnode = etree.SubElement(featurenode,"wkt")
    117        
     104            fieldnode.text = FieldString.decode(encoding="UTF-8", errors="strict")
     105
     106    wktnode = etree.SubElement(featurenode, "wkt")
     107
    118108    geom = feat.GetGeometryRef()       
    119109    wktnode.text = geom.ExportToWkt()   
    120                
    121110
    122111# Test the etree object