Skip to content

Commit 61dc8ea

Browse files
committedOct 12, 2017
[FEATURE][processing] New 'assign projection' algorithm
This algorithm assigns a new projection to a vector layer. It creates a new layer with the exact same features and geometries as the input one, but assigned to a new CRS. E.g. the geometries are not reprojected, they are just assigned to a different CRS. This algorithm can be used to repair layers which have been assigned an incorrect projection.
1 parent bd24c8b commit 61dc8ea

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
 
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ assign_projection.xsd"
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>0</gml:X><gml:Y>-5</gml:Y></gml:coord>
10+
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:assign_projection fid="points.0">
16+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
17+
<ogr:id>1</ogr:id>
18+
<ogr:id2>2</ogr:id2>
19+
</ogr:assign_projection>
20+
</gml:featureMember>
21+
<gml:featureMember>
22+
<ogr:assign_projection fid="points.1">
23+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geometryProperty>
24+
<ogr:id>2</ogr:id>
25+
<ogr:id2>1</ogr:id2>
26+
</ogr:assign_projection>
27+
</gml:featureMember>
28+
<gml:featureMember>
29+
<ogr:assign_projection fid="points.2">
30+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
31+
<ogr:id>3</ogr:id>
32+
<ogr:id2>0</ogr:id2>
33+
</ogr:assign_projection>
34+
</gml:featureMember>
35+
<gml:featureMember>
36+
<ogr:assign_projection fid="points.3">
37+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
38+
<ogr:id>4</ogr:id>
39+
<ogr:id2>2</ogr:id2>
40+
</ogr:assign_projection>
41+
</gml:featureMember>
42+
<gml:featureMember>
43+
<ogr:assign_projection fid="points.4">
44+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geometryProperty>
45+
<ogr:id>5</ogr:id>
46+
<ogr:id2>1</ogr:id2>
47+
</ogr:assign_projection>
48+
</gml:featureMember>
49+
<gml:featureMember>
50+
<ogr:assign_projection fid="points.5">
51+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>0,-5</gml:coordinates></gml:Point></ogr:geometryProperty>
52+
<ogr:id>6</ogr:id>
53+
<ogr:id2>0</ogr:id2>
54+
</ogr:assign_projection>
55+
</gml:featureMember>
56+
<gml:featureMember>
57+
<ogr:assign_projection fid="points.6">
58+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
59+
<ogr:id>7</ogr:id>
60+
<ogr:id2>0</ogr:id2>
61+
</ogr:assign_projection>
62+
</gml:featureMember>
63+
<gml:featureMember>
64+
<ogr:assign_projection fid="points.7">
65+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>7,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
66+
<ogr:id>8</ogr:id>
67+
<ogr:id2>0</ogr:id2>
68+
</ogr:assign_projection>
69+
</gml:featureMember>
70+
<gml:featureMember>
71+
<ogr:assign_projection fid="points.8">
72+
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
73+
<ogr:id>9</ogr:id>
74+
<ogr:id2>0</ogr:id2>
75+
</ogr:assign_projection>
76+
</gml:featureMember>
77+
</ogr:FeatureCollection>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
4+
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
5+
<xs:complexType name="FeatureCollectionType">
6+
<xs:complexContent>
7+
<xs:extension base="gml:AbstractFeatureCollectionType">
8+
<xs:attribute name="lockId" type="xs:string" use="optional"/>
9+
<xs:attribute name="scope" type="xs:string" use="optional"/>
10+
</xs:extension>
11+
</xs:complexContent>
12+
</xs:complexType>
13+
<xs:element name="assign_projection" type="ogr:assign_projection_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="assign_projection_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="id" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:long">
22+
<xs:totalDigits value="10"/>
23+
</xs:restriction>
24+
</xs:simpleType>
25+
</xs:element>
26+
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
27+
<xs:simpleType>
28+
<xs:restriction base="xs:long">
29+
<xs:totalDigits value="10"/>
30+
</xs:restriction>
31+
</xs:simpleType>
32+
</xs:element>
33+
</xs:sequence>
34+
</xs:extension>
35+
</xs:complexContent>
36+
</xs:complexType>
37+
</xs:schema>

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4148,3 +4148,15 @@ tests:
41484148
OUTPUT:
41494149
name: expected/extract_by_extent_clip.gml
41504150
type: vector
4151+
4152+
- algorithm: native:assignprojection
4153+
name: Assign projection
4154+
params:
4155+
CRS: EPSG:4283
4156+
INPUT:
4157+
name: custom/points.shp
4158+
type: vector
4159+
results:
4160+
OUTPUT:
4161+
name: expected/assign_projection.gml
4162+
type: vector

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
9292
addAlgorithm( new QgsRasterLayerUniqueValuesReportAlgorithm() );
9393
addAlgorithm( new QgsJoinByAttributeAlgorithm() );
9494
addAlgorithm( new QgsJoinWithLinesAlgorithm() );
95+
addAlgorithm( new QgsAssignProjectionAlgorithm() );
9596
}
9697

9798
void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
@@ -666,6 +667,37 @@ QgsFeature QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProces
666667
}
667668

668669

670+
QString QgsAssignProjectionAlgorithm::shortHelpString() const
671+
{
672+
return QObject::tr( "This algorithm assigns a new projection to a vector layer. It creates a new layer with the exact same features "
673+
"and geometries as the input one, but assigned to a new CRS. E.g. the geometries are not reprojected, they are just assigned "
674+
"to a different CRS. This algorithm can be used to repair layers which have been assigned an incorrect projection.\n\n"
675+
"Attributes are not modified by this algorithm." );
676+
}
677+
678+
QgsAssignProjectionAlgorithm *QgsAssignProjectionAlgorithm::createInstance() const
679+
{
680+
return new QgsAssignProjectionAlgorithm();
681+
}
682+
683+
void QgsAssignProjectionAlgorithm::initParameters( const QVariantMap & )
684+
{
685+
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Assigned CRS" ), QStringLiteral( "EPSG:4326" ) ) );
686+
}
687+
688+
bool QgsAssignProjectionAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
689+
{
690+
mDestCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
691+
return true;
692+
}
693+
694+
QgsFeature QgsAssignProjectionAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * )
695+
{
696+
return feature;
697+
}
698+
699+
700+
669701
void QgsSubdivideAlgorithm::initParameters( const QVariantMap & )
670702
{
671703
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MAX_NODES" ), QObject::tr( "Maximum nodes in parts" ), QgsProcessingParameterNumber::Integer,

‎src/core/processing/qgsnativealgorithms.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,39 @@ class QgsTransformAlgorithm : public QgsProcessingFeatureBasedAlgorithm
131131

132132
};
133133

134+
135+
136+
/**
137+
* Native assign projection algorithm.
138+
*/
139+
class QgsAssignProjectionAlgorithm : public QgsProcessingFeatureBasedAlgorithm
140+
{
141+
142+
public:
143+
144+
QgsAssignProjectionAlgorithm() = default;
145+
QString name() const override { return QStringLiteral( "assignprojection" ); }
146+
QString displayName() const override { return QObject::tr( "Assign projection" ); }
147+
virtual QStringList tags() const override { return QObject::tr( "assign,set,transform,reproject,crs,srs,warp" ).split( ',' ); }
148+
QString group() const override { return QObject::tr( "Vector general" ); }
149+
QString shortHelpString() const override;
150+
QgsAssignProjectionAlgorithm *createInstance() const override SIP_FACTORY;
151+
152+
protected:
153+
154+
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
155+
QgsCoordinateReferenceSystem outputCrs( const QgsCoordinateReferenceSystem & ) const override { return mDestCrs; }
156+
QString outputName() const override { return QObject::tr( "Assigned CRS" ); }
157+
158+
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
159+
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
160+
161+
private:
162+
163+
QgsCoordinateReferenceSystem mDestCrs;
164+
165+
};
166+
134167
/**
135168
* Native buffer algorithm.
136169
*/

0 commit comments

Comments
 (0)
Please sign in to comment.