Changes between Version 26 and Version 27 of BluePrint/GIS/ShapefileLayers


Ignore:
Timestamp:
05/17/13 13:36:49 (12 years ago)
Author:
Fran Boon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • BluePrint/GIS/ShapefileLayers

    v26 v27  
    3838
    3939=== Import into native Tables ===
     40
     41See below for code to convert SHP to an lxml.etree for import using XSLT
     42
    4043Paul Porthouse [2013-04-27 at the [http://mapaction.org MapAction] Hackathon]:
    4144
    42 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.
     45Our 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.
    4346
    4447A basic OGR2OGR which should accomplish the import is:
     
    5255{{{
    5356'''
    54 Dump the contents of a shapefile to an lxml etree object
    55 Assumes the shapefile is encoded in UTF-8 format
    56 Tested with TM_WORLD_BORDERS-0.3.shp
     57    Dump the contents of a shapefile to an lxml etree object
     58    Assumes the shapefile is encoded in UTF-8 format
     59    Tested with TM_WORLD_BORDERS-0.3.shp
    5760'''
     61
    5862import sys
    5963import ogr
     
    7074layername = os.path.splitext(os.path.basename(shapefilename))[0]
    7175
    72 #Create the datasource
     76# Create the datasource
    7377ds = ogr.Open( shapefilename )
    7478
    75 #Open the shapefile
     79# Open the shapefile
    7680if ds is None:
    7781    print "Open failed.\n"
    7882    sys.exit(0)
    7983   
    80 #Get the layer and iterate through the features
     84# Get the layer and iterate through the features
    8185lyr = ds.GetLayer(0)
    8286
     
    111115               
    112116
    113 #Test the etree object
     117# Test the etree object
    114118xmlString = etree.tostring(root, pretty_print=True)
    115119f = open("test.xml","w")
     
    117121}}}
    118122
    119 
    120123----
    121124[wiki:BluePrint/GIS GIS BluePrints]