mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[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.
This commit is contained in:
parent
bd24c8b664
commit
61dc8ea28f
77
python/plugins/processing/tests/testdata/expected/assign_projection.gml
vendored
Normal file
77
python/plugins/processing/tests/testdata/expected/assign_projection.gml
vendored
Normal file
@ -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/ assign_projection.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:assign_projection fid="points.0">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>1</ogr:id>
|
||||
<ogr:id2>2</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.1">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>2</ogr:id>
|
||||
<ogr:id2>1</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.2">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>3</ogr:id>
|
||||
<ogr:id2>0</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.3">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>4</ogr:id>
|
||||
<ogr:id2>2</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.4">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>5</ogr:id>
|
||||
<ogr:id2>1</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.5">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>0,-5</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>6</ogr:id>
|
||||
<ogr:id2>0</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.6">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>7</ogr:id>
|
||||
<ogr:id2>0</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.7">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>7,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>8</ogr:id>
|
||||
<ogr:id2>0</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
<gml:featureMember>
|
||||
<ogr:assign_projection fid="points.8">
|
||||
<ogr:geometryProperty><gml:Point srsName="EPSG:4283"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
|
||||
<ogr:id>9</ogr:id>
|
||||
<ogr:id2>0</ogr:id2>
|
||||
</ogr:assign_projection>
|
||||
</gml:featureMember>
|
||||
</ogr:FeatureCollection>
|
37
python/plugins/processing/tests/testdata/expected/assign_projection.xsd
vendored
Normal file
37
python/plugins/processing/tests/testdata/expected/assign_projection.xsd
vendored
Normal file
@ -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="assign_projection" type="ogr:assign_projection_Type" substitutionGroup="gml:_Feature"/>
|
||||
<xs:complexType name="assign_projection_Type">
|
||||
<xs:complexContent>
|
||||
<xs:extension base="gml:AbstractFeatureType">
|
||||
<xs:sequence>
|
||||
<xs:element name="geometryProperty" 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:long">
|
||||
<xs:totalDigits value="10"/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:element>
|
||||
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:long">
|
||||
<xs:totalDigits value="10"/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:extension>
|
||||
</xs:complexContent>
|
||||
</xs:complexType>
|
||||
</xs:schema>
|
@ -4148,3 +4148,15 @@ tests:
|
||||
OUTPUT:
|
||||
name: expected/extract_by_extent_clip.gml
|
||||
type: vector
|
||||
|
||||
- algorithm: native:assignprojection
|
||||
name: Assign projection
|
||||
params:
|
||||
CRS: EPSG:4283
|
||||
INPUT:
|
||||
name: custom/points.shp
|
||||
type: vector
|
||||
results:
|
||||
OUTPUT:
|
||||
name: expected/assign_projection.gml
|
||||
type: vector
|
||||
|
@ -92,6 +92,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
|
||||
addAlgorithm( new QgsRasterLayerUniqueValuesReportAlgorithm() );
|
||||
addAlgorithm( new QgsJoinByAttributeAlgorithm() );
|
||||
addAlgorithm( new QgsJoinWithLinesAlgorithm() );
|
||||
addAlgorithm( new QgsAssignProjectionAlgorithm() );
|
||||
}
|
||||
|
||||
void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
|
||||
@ -666,6 +667,37 @@ QgsFeature QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProces
|
||||
}
|
||||
|
||||
|
||||
QString QgsAssignProjectionAlgorithm::shortHelpString() const
|
||||
{
|
||||
return QObject::tr( "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.\n\n"
|
||||
"Attributes are not modified by this algorithm." );
|
||||
}
|
||||
|
||||
QgsAssignProjectionAlgorithm *QgsAssignProjectionAlgorithm::createInstance() const
|
||||
{
|
||||
return new QgsAssignProjectionAlgorithm();
|
||||
}
|
||||
|
||||
void QgsAssignProjectionAlgorithm::initParameters( const QVariantMap & )
|
||||
{
|
||||
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Assigned CRS" ), QStringLiteral( "EPSG:4326" ) ) );
|
||||
}
|
||||
|
||||
bool QgsAssignProjectionAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
|
||||
{
|
||||
mDestCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
|
||||
return true;
|
||||
}
|
||||
|
||||
QgsFeature QgsAssignProjectionAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * )
|
||||
{
|
||||
return feature;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void QgsSubdivideAlgorithm::initParameters( const QVariantMap & )
|
||||
{
|
||||
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MAX_NODES" ), QObject::tr( "Maximum nodes in parts" ), QgsProcessingParameterNumber::Integer,
|
||||
|
@ -131,6 +131,39 @@ class QgsTransformAlgorithm : public QgsProcessingFeatureBasedAlgorithm
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Native assign projection algorithm.
|
||||
*/
|
||||
class QgsAssignProjectionAlgorithm : public QgsProcessingFeatureBasedAlgorithm
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
QgsAssignProjectionAlgorithm() = default;
|
||||
QString name() const override { return QStringLiteral( "assignprojection" ); }
|
||||
QString displayName() const override { return QObject::tr( "Assign projection" ); }
|
||||
virtual QStringList tags() const override { return QObject::tr( "assign,set,transform,reproject,crs,srs,warp" ).split( ',' ); }
|
||||
QString group() const override { return QObject::tr( "Vector general" ); }
|
||||
QString shortHelpString() const override;
|
||||
QgsAssignProjectionAlgorithm *createInstance() const override SIP_FACTORY;
|
||||
|
||||
protected:
|
||||
|
||||
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
|
||||
QgsCoordinateReferenceSystem outputCrs( const QgsCoordinateReferenceSystem & ) const override { return mDestCrs; }
|
||||
QString outputName() const override { return QObject::tr( "Assigned CRS" ); }
|
||||
|
||||
bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
|
||||
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
|
||||
|
||||
private:
|
||||
|
||||
QgsCoordinateReferenceSystem mDestCrs;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Native buffer algorithm.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user