I have created a script to automatically generate base ANZLIC standard metadata using arcpy based coding. Can anyone help me convert it to OGR/GDAL python? This is so that it can be run in QGIS and work with all types of vector and raster data.

1st comment: You might get answers if you put your question more clearly. Rather than dumping your entire script in with some question fragments interspersed, try asking specific questions: How do I do the following in gdal/ogr? Open a raster/vector dataset, extract extent/spatial reference/other metadata etc...?
–
LukeOct 9 '12 at 23:27

2

2nd comment: You should restructure your code to make it easier to read, maintain and extend if need be. Pull the file system walking, metadata extraction, xml parsing into separate functions/class methods. Have a look at the MetaGETA project (code.google.com/p/metageta) for some examples. It's similar to what you are trying to do in that it generates ANZLIC metadata from filesystem rasters (disclaimer, I'm one of the authors).
–
LukeOct 9 '12 at 23:43

1 Answer
1

Unfortunately in OGR and GDAL there are no "List all datasets" functions available. This can be a bit of a pain, but it's easy enough to implement given a couple of provisos:

ogr.Open and gdal.Open will try and open any dataset, and will return None if
it fails (note, this doesn't throw an exception)

Many datasets are
actually made up of a number of different files on disk (such as ESRI
Shapefiles or MapInfo TAB). OGR can be given any of these files and
it will open the dataset (so, not just .shp, .shx and .dbf will all
open the one shapefile) - this means we need to ignore them manually.

At this point GDAL and OGR datasets behave differently, e.g. in OGR there are Layers, in GDAL there are Bands. Try using isinstance to look at the feature type and then do something with it. For example:

isinstance(gdal.Open(ds), gdal.Dataset)

Lastly you may have some issues with getting the projection info from your data; while the ogr.Layer.GetSpatialRef() returns a osr.SpatialReference object, with gdal.Dataset.GetProjection() just returns a wkt string. To get the spatial reference object from a GDAL wkt, you can use (from the Spatial Notes blog):

srs = osr.SpatialReference()
srs.ImportFromWkt(ds.GetProjection())

The projection may not have a name, or even an associated EPSG code, but you should be able to find out all the information you need from the Spatial Reference object once you have it.

One last, last note: rather than using the standard xml library, take a look at lxml instead. Particularly the E-Factory example in the tutorial. It should make building the XML a lot easier.