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?
-
1Probably there is some simple solution, but it is better to use gdal.vectorTranslate anyway. See usage example in gis.stackexchange.com/a/397049/30184.user30184– user301842024年03月06日 19:15:44 +00:00Commented Mar 6, 2024 at 19:15
-
1Maybe there are file names with spaces and ' - 's ?Pieter– Pieter2024年03月06日 21:10:59 +00:00Commented 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?i.i.k.– i.i.k.2024年03月06日 21:14:44 +00:00Commented Mar 6, 2024 at 21:14
-
@user30184 Can you elaborate on why it is?i.i.k.– i.i.k.2024年03月06日 21:35:39 +00:00Commented Mar 6, 2024 at 21:35
-
2To 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,...Pieter– Pieter2024年03月06日 22:03:27 +00:00Commented Mar 6, 2024 at 22:03
2 Answers 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')
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.