Skip to content

Commit fb73d0f

Browse files
committedFeb 1, 2012
[FEATURE]: creation of event layers in analysis lib using linear referencing
1 parent df2d801 commit fb73d0f

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
 

‎python/analysis/qgsgeometryanalyzer.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ class QgsGeometryAnalyzer
4646
bool onlySelectedFeatures = false,
4747
int uniqueIdField = -1 );
4848

49+
/**Creates an event layer (multipoint or multiline). Note that currently (until QgsGeometry supports m-values) the z-coordinate of the line layer is used for linear referencing
50+
@param lineLayer layer with the line geometry
51+
@param eventLayer layer with features and location field
52+
@param lineField join index in line layer
53+
@param eventField join index in event layer
54+
@param locationField1 attribute index of location field in event layer
55+
@param locationField2 attribute index of location end field (or -1 for point layer)
56+
*/
57+
bool eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer* eventLayer, int lineField, int eventField, const QString& outputLayer,
58+
const QString& outputFormat, int locationField1, int locationField2 = -1, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
59+
4960
private:
5061

5162
QList<double> simpleMeasure( QgsGeometry* geometry );

‎src/analysis/vector/qgsgeometryanalyzer.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,3 +908,103 @@ void QgsGeometryAnalyzer::bufferFeature( QgsFeature& f, int nProcessedFeatures,
908908
}
909909
}
910910
}
911+
912+
bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer* eventLayer, int lineField, int eventField, const QString& outputLayer,
913+
const QString& outputFormat, int locationField1, int locationField2, QgsVectorDataProvider* memoryProvider, QProgressDialog* p )
914+
{
915+
if ( !lineLayer || !eventLayer || !lineLayer->isValid() || !eventLayer->isValid() )
916+
{
917+
return false;
918+
}
919+
920+
//create line field / id map for line layer
921+
QHash< QString, QgsFeatureId > lineLayerIdMap;
922+
lineLayer->select( QgsAttributeList() << lineField,
923+
QgsRectangle(), false, false );
924+
QgsFeature fet;
925+
while ( lineLayer->nextFeature( fet ) )
926+
{
927+
lineLayerIdMap.insert( fet.attributeMap()[lineField].toString(), fet.id() );
928+
}
929+
930+
//create output datasource or attributes in memory provider
931+
QgsVectorFileWriter* fileWriter = 0;
932+
if ( !memoryProvider )
933+
{
934+
fileWriter = new QgsVectorFileWriter( outputLayer,
935+
eventLayer->dataProvider()->encoding(),
936+
eventLayer->pendingFields(),
937+
locationField2 == -1 ? QGis::WKBMultiPoint25D : QGis::WKBMultiLineString25D,
938+
&( lineLayer->crs() ),
939+
outputFormat );
940+
}
941+
else
942+
{
943+
memoryProvider->addAttributes( eventLayer->pendingFields().values() );
944+
}
945+
946+
//iterate over eventLayer and write new features to output file or layer
947+
eventLayer->select( eventLayer->pendingAllAttributesList(), QgsRectangle(), true, false );
948+
QgsGeometry* lrsGeom = 0;
949+
QgsFeature lineFeature;
950+
QgsGeometry* lineGeom = 0;
951+
double measure1, measure2;
952+
953+
while ( eventLayer->nextFeature( fet ) )
954+
{
955+
//get corresponding line feature
956+
QHash< QString, QgsFeatureId >::const_iterator layerIdIt = lineLayerIdMap.find( fet.attributeMap()[eventField].toString() );
957+
if ( layerIdIt == lineLayerIdMap.constEnd() )
958+
{
959+
continue;
960+
}
961+
if ( !lineLayer->featureAtId( *layerIdIt, lineFeature, true, false ) )
962+
{
963+
continue;
964+
}
965+
966+
measure1 = fet.attributeMap()[locationField1].toDouble();
967+
if ( locationField2 == -1 )
968+
{
969+
lrsGeom = locateAlongMeasure( measure1, lineFeature.geometry() );
970+
}
971+
else
972+
{
973+
measure2 = fet.attributeMap()[locationField2].toDouble();
974+
lrsGeom = locateBetweenMeasures( measure1, measure2, lineFeature.geometry() );
975+
}
976+
977+
if ( lrsGeom )
978+
{
979+
fet.setGeometry( lrsGeom );
980+
if ( memoryProvider )
981+
{
982+
memoryProvider->addFeatures( QgsFeatureList() << fet );
983+
}
984+
else
985+
{
986+
fileWriter->addFeature( fet );
987+
}
988+
}
989+
}
990+
991+
return true;
992+
}
993+
994+
QgsGeometry* QgsGeometryAnalyzer::locateAlongMeasure( double measure, const QgsGeometry* lineGeom )
995+
{
996+
//only for testing
997+
QgsGeometry* geom = new QgsGeometry();
998+
*geom = *lineGeom;
999+
geom->convertToMultiType();
1000+
return geom;
1001+
}
1002+
1003+
QgsGeometry* QgsGeometryAnalyzer::locateBetweenMeasures( double fromMeasure, double toMeasure, const QgsGeometry* lineGeom )
1004+
{
1005+
//only for testing
1006+
QgsGeometry* geom = new QgsGeometry();
1007+
*geom = *lineGeom;
1008+
geom->convertToMultiType();
1009+
return geom;
1010+
}

‎src/analysis/vector/qgsgeometryanalyzer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ class ANALYSIS_EXPORT QgsGeometryAnalyzer
100100
bool dissolve( QgsVectorLayer* layer, const QString& shapefileName, bool onlySelectedFeatures = false,
101101
int uniqueIdField = -1, QProgressDialog* p = 0 );
102102

103+
/**Creates an event layer (multipoint or multiline). Note that currently (until QgsGeometry supports m-values) the z-coordinate of the line layer is used for linear referencing
104+
@param lineLayer layer with the line geometry
105+
@param eventLayer layer with features and location field
106+
@param lineField join index in line layer
107+
@param eventField join index in event layer
108+
@param locationField1 attribute index of location field in event layer
109+
@param locationField2 attribute index of location end field (or -1 for point layer)
110+
*/
111+
bool eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer* eventLayer, int lineField, int eventField, const QString& outputLayer,
112+
const QString& outputFormat, int locationField1, int locationField2 = -1, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
113+
103114
private:
104115

105116
QList<double> simpleMeasure( QgsGeometry* geometry );
@@ -115,6 +126,8 @@ class ANALYSIS_EXPORT QgsGeometryAnalyzer
115126
void convexFeature( QgsFeature& f, int nProcessedFeatures, QgsGeometry** dissolveGeometry );
116127
/**Helper function to dissolve feature(s)*/
117128
void dissolveFeature( QgsFeature& f, int nProcessedFeatures, QgsGeometry** dissolveGeometry );
129+
QgsGeometry* locateAlongMeasure( double measure, const QgsGeometry* lineGeom );
130+
QgsGeometry* locateBetweenMeasures( double fromMeasure, double toMeasure, const QgsGeometry* lineGeom );
118131

119132
};
120133
#endif //QGSVECTORANALYZER

0 commit comments

Comments
 (0)
Please sign in to comment.