0

I am using ogr2ogr to load data from multiple GML files into PostGIS. As shown in the ogrinfo output below, each GML file has multiple layers.

    Had to open data source read-only.
INFO: Open of `D:\Projects\osmaps\data\vectorMapDistrict_unzipped\HP40.gml'
      using driver `GML' successful.
1: Ornament (Polygon)
2: SurfaceWater_Line (Line String)
3: TidalWater (Polygon)
4: SpotHeight (Point)
5: TidalBoundary (Line String)
6: FunctionalSite (Point)
7: SurfaceWater_Area (Polygon)
8: Road (Line String)
9: Foreshore (Polygon)
10: AdministrativeBoundary (Line String)
11: NamedPlace (Point)
12: Building (Polygon)

I want to load all layers except the Ornament layer. How do I get ogr2ogr to skip the specific layer?

My current script in Windows Powershell:

# Input gml directory
$gmlDir = "D:\Projects\osmaps\data\vectorMapDistrict_unzipped"
# Loop through gml files
Foreach($gmlFile in Get-ChildItem $gmlDir -filter *.gml) {
    #Load data
    ogr2ogr -f PostgreSQL PG:"dbname=osmaps host=localhost port=5432 active_schema=vectormapdistrict user=postgres" -append -progress --config PG_USE_COPY YES $gmlFile.fullname}

2 Answers 2

2

You can't skip layers but you can select explicitly the layers which you want to keep with ogr2ogr command by giving a list of layers after the datasource.

This should work:

ogr2ogr -f PostgreSQL PG:"dbname=osmaps host=localhost port=5432 active_schema=vectormapdistrict user=postgres" -append -progress --config PG_USE_COPY YES $gmlFile.fullname} SurfaceWater_Line TidalWater SpotHeight TidalBoundary FunctionalSite SurfaceWater_Area Road Foreshore AdministrativeBoundary NamedPlace Building
3
  • Not all the GML files will have all the layers. So if a layer is not present in the GML I get an error e.g.: "FAILURE: Couldn't fetch requested layer 'Foreshore'".
    – HishivS
    Commented Dec 7, 2015 at 15:49
  • 1
    You can have a try with -skipfailures but I fear it skips only invalid features, not layers.
    – user30184
    Commented Dec 7, 2015 at 16:16
  • Yes, the -skipfailures switch solves it.
    – HishivS
    Commented Dec 7, 2015 at 16:24
1

You can't skip layers but you can select explicitly the layers which you want to keep by writing a VRT file http://www.gdal.org/drv_vrt.html. For your data this file should expose only one layer from your GML

<OGRVRTDataSource>
    <OGRVRTLayer name="SurfaceWater_Line">
        <SrcDataSource>HP40.gml</SrcDataSource>
        <SrcLayer>SurfaceWater_Line</SrcLayer>
    </OGRVRTLayer>
</OGRVRTDataSource>

Save as filtered_gml.xml and test with ogrinfo filtered_gml.xml. Go on and add more OGRVRTLayer elements.

3
  • This would work on a single GML file. I need to loop through multiple GML files, and load each one.
    – HishivS
    Commented Dec 7, 2015 at 15:16
  • Make your loop to edit <SrcDataSource>. I would probably just drop the ornament table at the end but of course it would mean a little bit slower processing.
    – user30184
    Commented Dec 7, 2015 at 15:18
  • Sorry, all too complicated. I will write a new answer and delete this one after a moment.
    – user30184
    Commented Dec 7, 2015 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.