Changes between Version 33 and Version 34 of BluePrint/GIS/ShapefileLayers
- Timestamp:
- 05/17/13 18:50:57 (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
BluePrint/GIS/ShapefileLayers
v33 v34 43 43 44 44 === Import into native Tables === 45 SHP should be converted to an lxml.etree for import using XSLT. 45 46 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 47 Working 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 59 49 60 50 {{{ … … 74 64 print "Usage: importshape.py filename" 75 65 sys.exit(0) 76 66 77 67 shapefilename = sys.argv[1] 78 68 … … 80 70 81 71 # Create the datasource 82 ds = ogr.Open( shapefilename)72 ds = ogr.Open(shapefilename) 83 73 84 74 # Open the shapefile … … 86 76 print "Open failed.\n" 87 77 sys.exit(0) 88 78 89 79 # Get the layer and iterate through the features 90 80 lyr = ds.GetLayer(0) 91 81 92 root = etree.Element("shapefile", name=layername)82 root = etree.Element("shapefile", name=layername) 93 83 94 84 for feat in lyr: 95 96 featurenode = etree.SubElement(root, "feature")97 85 86 featurenode = etree.SubElement(root, "feature") 87 98 88 feat_defn = lyr.GetLayerDefn() 99 89 100 90 for i in range(feat_defn.GetFieldCount()): 101 91 102 92 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 106 96 if field_defn.GetType() == ogr.OFTInteger: 107 97 fieldnode.text = str(feat.GetFieldAsInteger(i)) … … 112 102 elif field_defn.GetType() == ogr.OFTString: 113 103 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 118 108 geom = feat.GetGeometryRef() 119 109 wktnode.text = geom.ExportToWkt() 120 121 110 122 111 # Test the etree object