Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter to skip layername in gdal:convertformat #45955

Merged
merged 10 commits into from
Dec 9, 2021
13 changes: 12 additions & 1 deletion python/plugins/processing/algs/gdal/ogr2ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from qgis.core import (QgsProcessing,
QgsProcessingException,
QgsProcessingParameterBoolean,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
Expand All @@ -35,6 +36,7 @@

class ogr2ogr(GdalAlgorithm):
INPUT = 'INPUT'
CONVERT_ALL_LAYERS = 'CONVERT_ALL_LAYERS'
OPTIONS = 'OPTIONS'
OUTPUT = 'OUTPUT'

Expand All @@ -46,6 +48,9 @@ def initAlgorithm(self, config=None):
self.tr('Input layer'),
types=[QgsProcessing.TypeVector]))

self.addParameter(QgsProcessingParameterBoolean(self.CONVERT_ALL_LAYERS,
self.tr('Convert all layers from dataset'), defaultValue=False))

options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'),
defaultValue='',
Expand Down Expand Up @@ -73,6 +78,7 @@ def commandName(self):

def getConsoleCommands(self, parameters, context, feedback, executing=True):
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
convertAllLayers = self.parameterAsBoolean(parameters, self.CONVERT_ALL_LAYERS, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, outFile)
Expand All @@ -91,6 +97,11 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments.append(output)
arguments.append(ogrLayer)
arguments.append(layerName)
if not convertAllLayers:
arguments.append(layerName)

return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

def shortHelpString(self):
return self.tr("The algorithm converts simple features data between file formats.\n\n"
"Use convert all layers to convert a whole dataset.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ results_convert_all_layers_points_lines.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>-1</gml:X><gml:Y>-5</gml:Y></gml:coord>
<gml:coord><gml:X>11</gml:X><gml:Y>5</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:points fid="points.0">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>1</ogr:id>
<ogr:id2>2</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.1">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>2</ogr:id>
<ogr:id2>1</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.2">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>3</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.3">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>4</ogr:id>
<ogr:id2>2</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.4">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>5</ogr:id>
<ogr:id2>1</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.5">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-5</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>6</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.6">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>7</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.7">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>7,-1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>8</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.8">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>9</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.0">
<ogr:geom><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,2 9,2 9,3 11,5</gml:coordinates></gml:LineString></ogr:geom>
</ogr:lines>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.1">
<ogr:geom><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geom>
</ogr:lines>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.2">
<ogr:geom><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></ogr:geom>
</ogr:lines>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.3">
<ogr:geom><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></ogr:geom>
</ogr:lines>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.4">
<ogr:geom><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 10,-3</gml:coordinates></gml:LineString></ogr:geom>
</ogr:lines>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.5">
<ogr:geom><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 10,1</gml:coordinates></gml:LineString></ogr:geom>
</ogr:lines>
</gml:featureMember>
<gml:featureMember>
<ogr:lines fid="lines.6">
</ogr:lines>
</gml:featureMember>
</ogr:FeatureCollection>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="points" type="ogr:points_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="points_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geom" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="id" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="16"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="16"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="lines" type="ogr:lines_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="lines_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geom" type="gml:LineStringPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ results_convert_only_one_layer_points.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>0</gml:X><gml:Y>-5</gml:Y></gml:coord>
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:points fid="points.0">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>1</ogr:id>
<ogr:id2>2</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.1">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>2</ogr:id>
<ogr:id2>1</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.2">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>3</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.3">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>4</ogr:id>
<ogr:id2>2</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.4">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>5</ogr:id>
<ogr:id2>1</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.5">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-5</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>6</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.6">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>7</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.7">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>7,-1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>8</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
<gml:featureMember>
<ogr:points fid="points.8">
<ogr:geom><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geom>
<ogr:id>9</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:points>
</gml:featureMember>
</ogr:FeatureCollection>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="points" type="ogr:points_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="points_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geom" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="id" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="16"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="16"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,29 @@ tests:
OUTPUT:
name: expected/gdal/dissolved_points.gml
type: vector

- algorithm: gdal:convertformat
name: Convert only one layer from GPKG to GML (gdal:convertformat)
params:
CONVERT_ALL_LAYERS: false
INPUT:
name: points_lines.gpkg|layername=points
type: vector
OPTIONS: ''
results:
OUTPUT:
name: expected/results_convert_only_one_layer_points.gml
type: vector

- algorithm: gdal:convertformat
name: Convert all layers from GPKG to GML (gdal:convertformat)
params:
CONVERT_ALL_LAYERS: true
INPUT:
name: points_lines.gpkg|layername=points
type: vector
OPTIONS: ''
results:
OUTPUT:
name: expected/results_convert_all_layers_points_lines.gml
type: vector
Binary file not shown.