Skip to content

Commit 2d4a596

Browse files
committedMar 21, 2012
Add possibility to force eventLayer features to single geometry type
1 parent c27c890 commit 2d4a596

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed
 

‎python/analysis/qgsgeometryanalyzer.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class QgsGeometryAnalyzer
6060
@param p progress dialog or 0 if no progress dialog should be shown
6161
*/
6262
bool eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer* eventLayer, int lineField, int eventField, QList<int>& unlocatedFeatureIds /Out/,
63-
const QString& outputLayer, const QString& outputFormat, int locationField1, int locationField2 = -1, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
63+
const QString& outputLayer, const QString& outputFormat, int locationField1, int locationField2 = -1, bool forceSingleGeometry = false,
64+
QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
6465

6566
/**Returns multilinestring*/
6667
QgsGeometry* locateBetweenMeasures( double fromMeasure, double toMeasure, QgsGeometry* lineGeom );

‎src/analysis/vector/qgsgeometryanalyzer.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ void QgsGeometryAnalyzer::bufferFeature( QgsFeature& f, int nProcessedFeatures,
910910
}
911911

912912
bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer* eventLayer, int lineField, int eventField, QList<int>& unlocatedFeatureIds, const QString& outputLayer,
913-
const QString& outputFormat, int locationField1, int locationField2, QgsVectorDataProvider* memoryProvider, QProgressDialog* p )
913+
const QString& outputFormat, int locationField1, int locationField2, bool forceSingleGeometry, QgsVectorDataProvider* memoryProvider, QProgressDialog* p )
914914
{
915915
if ( !lineLayer || !eventLayer || !lineLayer->isValid() || !eventLayer->isValid() )
916916
{
@@ -932,10 +932,19 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
932932
QgsFeatureList memoryProviderFeatures;
933933
if ( !memoryProvider )
934934
{
935+
QGis::WkbType memoryProviderType = QGis::WKBMultiLineString;
936+
if ( locationField2 == -1 )
937+
{
938+
memoryProviderType = forceSingleGeometry ? QGis::WKBPoint : QGis::WKBMultiPoint;
939+
}
940+
else
941+
{
942+
memoryProviderType = forceSingleGeometry ? QGis::WKBLineString : QGis::WKBMultiLineString;
943+
}
935944
fileWriter = new QgsVectorFileWriter( outputLayer,
936945
eventLayer->dataProvider()->encoding(),
937946
eventLayer->pendingFields(),
938-
locationField2 == -1 ? QGis::WKBMultiPoint : QGis::WKBMultiLineString,
947+
memoryProviderType,
939948
&( lineLayer->crs() ),
940949
outputFormat );
941950
}
@@ -1003,15 +1012,7 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
10031012
if ( lrsGeom )
10041013
{
10051014
++nOutputFeatures;
1006-
fet.setGeometry( lrsGeom );
1007-
if ( memoryProvider )
1008-
{
1009-
memoryProviderFeatures << fet;
1010-
}
1011-
else if ( fileWriter )
1012-
{
1013-
fileWriter->addFeature( fet );
1014-
}
1015+
addEventLayerFeature( fet, lrsGeom, fileWriter, memoryProviderFeatures, forceSingleGeometry );
10151016
}
10161017
}
10171018
if ( nOutputFeatures < 1 )
@@ -1033,6 +1034,43 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
10331034
return true;
10341035
}
10351036

1037+
void QgsGeometryAnalyzer::addEventLayerFeature( QgsFeature& feature, QgsGeometry* geom, QgsVectorFileWriter* fileWriter, QgsFeatureList& memoryFeatures, bool forceSingleType )
1038+
{
1039+
if ( !geom )
1040+
{
1041+
return;
1042+
}
1043+
1044+
QList<QgsGeometry*> geomList;
1045+
if ( forceSingleType )
1046+
{
1047+
geomList = geom->asGeometryCollection();
1048+
}
1049+
else
1050+
{
1051+
geomList.push_back( geom );
1052+
}
1053+
1054+
QList<QgsGeometry*>::iterator geomIt = geomList.begin();
1055+
for ( ; geomIt != geomList.end(); ++geomIt )
1056+
{
1057+
feature.setGeometry( *geomIt );
1058+
if ( fileWriter )
1059+
{
1060+
fileWriter->addFeature( feature );
1061+
}
1062+
else
1063+
{
1064+
memoryFeatures << feature;
1065+
}
1066+
}
1067+
1068+
if ( forceSingleType )
1069+
{
1070+
delete geom;
1071+
}
1072+
}
1073+
10361074
QgsGeometry* QgsGeometryAnalyzer::locateBetweenMeasures( double fromMeasure, double toMeasure, QgsGeometry* lineGeom )
10371075
{
10381076
if ( !lineGeom )

‎src/analysis/vector/qgsgeometryanalyzer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ class ANALYSIS_EXPORT QgsGeometryAnalyzer
111111
@param unlocatedFeatureIds out: ids of event features where linear referencing was not successful
112112
@param locationField1 attribute index of location field in event layer
113113
@param locationField2 attribute index of location end field (or -1 for point layer)
114+
@param forceSingleGeometry force layer to single point/line type. Feature attributes are copied in case of multiple matches
114115
@param memoryProvider memory provider to write output to (can be 0 if output is written to a file)
115116
@param p progress dialog or 0 if no progress dialog should be shown
116117
*/
117118
bool eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer* eventLayer, int lineField, int eventField, QList<int>& unlocatedFeatureIds, const QString& outputLayer,
118-
const QString& outputFormat, int locationField1, int locationField2 = -1, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
119+
const QString& outputFormat, int locationField1, int locationField2 = -1, bool forceSingleGeometry = false, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );
119120

120121
/**Returns linear reference geometry as a multiline (or 0 if no match). Currently, the z-coordinates are considered to be the measures (no support for m-values in QGIS)*/
121122
QgsGeometry* locateBetweenMeasures( double fromMeasure, double toMeasure, QgsGeometry* lineGeom );
@@ -140,7 +141,7 @@ class ANALYSIS_EXPORT QgsGeometryAnalyzer
140141
void dissolveFeature( QgsFeature& f, int nProcessedFeatures, QgsGeometry** dissolveGeometry );
141142

142143
//helper functions for event layer
143-
144+
void addEventLayerFeature( QgsFeature& feature, QgsGeometry* geom, QgsVectorFileWriter* fileWriter, QgsFeatureList& memoryFeatures, bool forceSingleType = false );
144145
unsigned char* locateBetweenWkbString( unsigned char* ptr, QgsMultiPolyline& result, double fromMeasure, double toMeasure );
145146
unsigned char* locateAlongWkbString( unsigned char* ptr, QgsMultiPoint& result, double measure );
146147
static bool clipSegmentByRange( double x1, double y1, double m1, double x2, double y2, double m2, double range1, double range2, QgsPoint& pt1, QgsPoint& pt2, bool& secondPointClipped );

0 commit comments

Comments
 (0)
Please sign in to comment.