1

I have 4126 GML-files as.xml, containing building polygons. I want to use ogr2ogr to transform them to geopackages. The following command works on one layer when I run it in the OS4Geo Shell shipped with QGIS:

ogr2ogr -f GPKG c:\path\to\ouput.gpkg GMLAS:c:\path\to\input.xml -oo REMOVE_UNUSED_FIELDS=YES

I tried to adapt this to a python script, where I use it in the following way

outputfilepath = r"path\to\LoD1.gpkg"
inputfilepath = r"path\to\LoD1.xml"
os.system("ogr2ogr -f GPKG "+outputfilepath+" GMLAS:"+inputfilepath+" -oo REMOVE_UNUSED_FIELDS=YES")

However, nothing happens, I don't even get an error message.

When I run this in the command line with a running python instance I get the error message
ERROR 6: Unknown option name '-'

What am I doing wrong?

asked Mar 6, 2024 at 13:42
6
  • 1
    Probably there is some simple solution, but it is better to use gdal.vectorTranslate anyway. See usage example in gis.stackexchange.com/a/397049/30184. Commented Mar 6, 2024 at 19:15
  • 1
    Maybe there are file names with spaces and ' - 's ? Commented Mar 6, 2024 at 21:10
  • @Pieter Yes, there are namespaces and also '-' in the name spaces. So is the command correctly written as I did above? So the error must be in the namespaces? Commented Mar 6, 2024 at 21:14
  • @user30184 Can you elaborate on why it is? Commented Mar 6, 2024 at 21:35
  • 2
    To add more elaboration pro gdal.vectorTranslate... If one-shot it's not that important, but if used regularly: you avoid issues like this one, it is more readable because you pass the seperate parameters rather than concatenating a long command line string, it will be significantly faster for many (small) files because no seperate process will be started for each file converted, you are able to e.g. use different gdal versions in e.g. conda envs in a clean way,... Commented Mar 6, 2024 at 22:03

2 Answers 2

2

When starting an application on the command line, command line interpreters will pass everything separated by spaces as separate parameters to the application (ogr2ogr in this case).

When there are spaces in the paths you pass as parameter, these spaces in them will also be interpreted as separators between parameters, which leads to weird parameters, and errors. In your case, with the error unknown option name "-", this seems to signal that there are paths with constructs like ' - ' in them, leading to the '-' between spaces being interpreted as a seperate (invalid) parameter rather than a part of a path.

If there are double quotes around a parameter (e.g. around your file paths), typically command line interpreters will ignore the spaces between the double quotes... So something like this should work:

outputfilepath = r"path\to\LoD1.gpkg"
inputfilepath = r"path\to\LoD1.xml"
os.system('ogr2ogr -f GPKG "'+outputfilepath+'" "GMLAS:'+inputfilepath+'" -oo REMOVE_UNUSED_FIELDS=YES')
answered Mar 6, 2024 at 21:22
2
  • Thank you very much! I will try it out! Commented Mar 6, 2024 at 21:24
  • It did work!! Thank you very much! Commented Mar 11, 2024 at 11:01
0

Based on @Pieter's answer I adjusted my code accordingly:

# folder to all input files
inputpath = r'path\to\inputfolder'
# iterate over folder and store all file names in a list
list_of_files = os.listdir(inputpath)
#path to output folder as string
outputpath = r'path\to\outputfolder'
# iterate the list of files and create names for output
for file in list_of_files:
 
 if "xml" in file:
 
 inputfilepath = inputpath + "\\"+ file
 
 # Create path of output file based on the input file name
 filename = file.split('.')[0]
 outputfile = filename + '_ogr.gpkg'
 outputfilepath = outputpath+"\\"+ outputfile
 
 # define the ogr2ogr command, here we transform .xml to gpkg and we set the CRS to epsg:25832
 command = 'ogr2ogr -f GPKG "'+outputfilepath+'" -a_srs "EPSG:25832" "GMLAS:'+inputfilepath+'" -oo REMOVE_UNUSED_FIELDS=YES'
 
 # Run the command
 os.system(command)

For the 4000 files this takes a while until the files are all transformed.

answered Mar 11, 2024 at 12:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.