Skip to content

Commit 564ca7b

Browse files
author
cfarmer
committed
Fix crashes due to geometry pointers in QgsGeometryAnalyzer; Add QgsOverlayAnalyzer to analysis library (currently only supports intersections)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12040 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c4f936b commit 564ca7b

File tree

5 files changed

+441
-48
lines changed

5 files changed

+441
-48
lines changed

python/analysis/analysis.sip

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
%Import core/core.sip
99

1010
%Include qgsgeometryanalyzer.sip
11+
%Include qgsoverlayanalyzer.sip
1112

src/analysis/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SET(QGIS_ANALYSIS_SRCS
3131
raster/qgstotalcurvaturefilter.cpp
3232
vector/qgsgeometryanalyzer.cpp
3333
vector/qgszonalstatistics.cpp
34+
vector/qgsoverlayanalyzer.cpp
3435
)
3536

3637
SET(QGIS_ANALYSIS_MOC_HDRS
@@ -43,6 +44,7 @@ INCLUDE_DIRECTORIES(
4344
${CMAKE_CURRENT_SOURCE_DIR}
4445
${CMAKE_CURRENT_SOURCE_DIR}/../core/
4546
${CMAKE_CURRENT_SOURCE_DIR}/../core/renderer
47+
${CMAKE_CURRENT_SOURCE_DIR}/../core/spatialindex
4648
interpolation
4749
${PROJ_INCLUDE_DIR}
4850
${GEOS_INCLUDE_DIR}
@@ -99,7 +101,7 @@ INSTALL(TARGETS qgis_analysis
99101

100102
# Added by Tim to install headers
101103

102-
SET(QGIS_ANALYSIS_HDRS vector/qgsgeometryanalyzer.h vector/qgszonalstatistics.h
104+
SET(QGIS_ANALYSIS_HDRS vector/qgsgeometryanalyzer.h vector/qgszonalstatistics.h vector/qgsgeometryanalyzer.h
103105
)
104106

105107
INSTALL(CODE "MESSAGE(\"Installing ANALYSIS headers...\")")

src/analysis/vector/qgsgeometryanalyzer.cpp

+128-47
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ bool QgsGeometryAnalyzer::extent( QgsVectorLayer* layer, const QString& shapefil
329329
QList<double> QgsGeometryAnalyzer::simpleMeasure( QgsGeometry* mpGeometry )
330330
{
331331
QList<double> list;
332+
double perim;
332333
if ( mpGeometry->wkbType() == QGis::WKBPoint )
333334
{
334335
QgsPoint pt = mpGeometry->asPoint();
@@ -341,11 +342,11 @@ QList<double> QgsGeometryAnalyzer::simpleMeasure( QgsGeometry* mpGeometry )
341342
list.append( measure.measure( mpGeometry ) );
342343
if ( mpGeometry->type() == QGis::Polygon )
343344
{
344-
list.append( perimeterMeasure( mpGeometry, measure ) );
345+
perim = perimeterMeasure( mpGeometry, measure );
346+
list.append( perim );
345347
}
346348
}
347349
return list;
348-
349350
}
350351

351352
double QgsGeometryAnalyzer::perimeterMeasure( QgsGeometry* geometry, QgsDistanceArea& measure )
@@ -383,51 +384,90 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
383384
{
384385
return false;
385386
}
386-
387387
QgsVectorDataProvider* dp = layer->dataProvider();
388388
if ( !dp )
389389
{
390390
return false;
391391
}
392+
bool useField = false;
393+
if ( uniqueIdField == -1 )
394+
{
395+
uniqueIdField = 0;
396+
}
397+
else
398+
{
399+
useField = true;
400+
}
401+
QgsFieldMap fields;
402+
fields.insert( 0 , QgsField( QString( "UID" ), QVariant::String ) );
403+
fields.insert( 1 , QgsField( QString( "AREA" ), QVariant::Double ) );
404+
fields.insert( 2 , QgsField( QString( "PERIM" ), QVariant::Double ) );
392405

393406
QGis::WkbType outputType = QGis::WKBPolygon;
394407
const QgsCoordinateReferenceSystem crs = layer->srs();
395408

396-
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), dp->fields(), outputType, &crs );
409+
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), fields, outputType, &crs );
397410
QgsFeature currentFeature;
398411
QgsGeometry* dissolveGeometry; //dissolve geometry
399412
QMultiMap<QString, int> map;
400-
bool useField = false;
401413

402-
if ( uniqueIdField == -1 )
414+
if ( onlySelectedFeatures )
403415
{
404-
uniqueIdField = 0;
416+
//use QgsVectorLayer::featureAtId
417+
const QgsFeatureIds selection = layer->selectedFeaturesIds();
418+
QgsFeatureIds::const_iterator it = selection.constBegin();
419+
for ( ; it != selection.constEnd(); ++it )
420+
{
421+
// if ( p )
422+
// {
423+
// p->setValue( processedFeatures );
424+
// }
425+
// if ( p && p->wasCanceled() )
426+
// {
427+
// // break; // it may be better to do something else here?
428+
// return false;
429+
// }
430+
if ( !layer->featureAtId( *it, currentFeature, true, true ) )
431+
{
432+
continue;
433+
}
434+
map.insert( currentFeature.attributeMap()[ uniqueIdField ].toString(), currentFeature.id() );
435+
}
405436
}
406437
else
407438
{
408-
useField = true;
409439
layer->select( layer->pendingAllAttributesList(), QgsRectangle(), true, false );
410440
while ( layer->nextFeature( currentFeature ) )
411441
{
412-
map.insert( currentFeature.attributeMap()[uniqueIdField].toString(), currentFeature.id() );
442+
// if ( p )
443+
// {
444+
// p->setValue( processedFeatures );
445+
// }
446+
// if ( p && p->wasCanceled() )
447+
// {
448+
// // break; // it may be better to do something else here?
449+
// return false;
450+
// }
451+
map.insert( currentFeature.attributeMap()[ uniqueIdField ].toString(), currentFeature.id() );
413452
}
414453
}
415-
QMultiMap<QString, int>::const_iterator jt;
416-
for (jt = map.constBegin(); jt != map.constEnd(); ++jt)
454+
455+
QMultiMap<QString, int>::const_iterator jt = map.constBegin();
456+
while ( jt != map.constEnd() )
417457
{
418458
QString currentKey = jt.key();
419459
int processedFeatures = 0;
420460
//take only selection
421461
if ( onlySelectedFeatures )
422462
{
423463
//use QgsVectorLayer::featureAtId
424-
const QgsFeatureIds selection = layer->selectedFeaturesIds();
464+
const QgsFeatureIds selection = layer->selectedFeaturesIds();
425465
if ( p )
426466
{
427-
p->setMaximum( selection.size() );
467+
p->setMaximum( selection.size() );
428468
}
429469
processedFeatures = 0;
430-
while ( jt != map.end() && ( jt.key() == currentKey || !useField ) )
470+
while ( jt != map.constEnd() && ( jt.key() == currentKey || !useField ) )
431471
{
432472
if ( p && p->wasCanceled() )
433473
{
@@ -437,18 +477,26 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
437477
{
438478
if ( p )
439479
{
440-
p->setValue( processedFeatures );
480+
p->setValue( processedFeatures );
441481
}
442482
if ( !layer->featureAtId( jt.value(), currentFeature, true, true ) )
443483
{
444-
continue;
484+
continue;
445485
}
446486
convexFeature( currentFeature, processedFeatures, &dissolveGeometry );
447487
++processedFeatures;
448488
}
449489
++jt;
450490
}
491+
QList<double> values;
492+
dissolveGeometry = dissolveGeometry->convexHull();
493+
values = simpleMeasure( dissolveGeometry );
494+
QgsAttributeMap attributeMap;
495+
attributeMap.insert( 0 , QVariant( currentKey ) );
496+
attributeMap.insert( 1 , values[ 0 ] );
497+
attributeMap.insert( 2 , values[ 1 ] );
451498
QgsFeature dissolveFeature;
499+
dissolveFeature.setAttributeMap( attributeMap );
452500
dissolveFeature.setGeometry( dissolveGeometry );
453501
vWriter.addFeature( dissolveFeature );
454502
}
@@ -461,7 +509,7 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
461509
p->setMaximum( featureCount );
462510
}
463511
processedFeatures = 0;
464-
while ( jt != map.end() && ( jt.key() == currentKey || !useField ) )
512+
while ( jt != map.constEnd() && ( jt.key() == currentKey || !useField ) )
465513
{
466514
if ( p )
467515
{
@@ -480,9 +528,19 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
480528
++processedFeatures;
481529
++jt;
482530
}
483-
QgsFeature dissolveFeature;
484-
dissolveFeature.setGeometry( dissolveGeometry );
485-
vWriter.addFeature( dissolveFeature );
531+
QList<double> values;
532+
// QgsGeometry* tmpGeometry = 0;
533+
dissolveGeometry = dissolveGeometry->convexHull();
534+
// values = simpleMeasure( tmpGeometry );
535+
values = simpleMeasure( dissolveGeometry );
536+
QgsAttributeMap attributeMap;
537+
attributeMap.insert( 0 , QVariant( currentKey ) );
538+
attributeMap.insert( 1 , QVariant( values[ 0 ] ) );
539+
attributeMap.insert( 2 , QVariant( values[ 1 ] ) );
540+
QgsFeature dissolveFeature;
541+
dissolveFeature.setAttributeMap( attributeMap );
542+
dissolveFeature.setGeometry( dissolveGeometry );
543+
vWriter.addFeature( dissolveFeature );
486544
}
487545
}
488546
return true;
@@ -522,51 +580,69 @@ bool QgsGeometryAnalyzer::dissolve( QgsVectorLayer* layer, const QString& shapef
522580
{
523581
return false;
524582
}
525-
526583
QgsVectorDataProvider* dp = layer->dataProvider();
527584
if ( !dp )
528585
{
529586
return false;
530587
}
588+
bool useField = false;
589+
if ( uniqueIdField == -1 )
590+
{
591+
uniqueIdField = 0;
592+
}
593+
else
594+
{
595+
useField = true;
596+
}
531597

532598
QGis::WkbType outputType = dp->geometryType();
533599
const QgsCoordinateReferenceSystem crs = layer->srs();
534600

535601
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), dp->fields(), outputType, &crs );
536602
QgsFeature currentFeature;
537-
QgsGeometry* dissolveGeometry; //dissolve geometry
538603
QMultiMap<QString, int> map;
539-
bool useField = false;
540604

541-
if ( uniqueIdField == -1 )
605+
if ( onlySelectedFeatures )
542606
{
543-
uniqueIdField = 0;
607+
//use QgsVectorLayer::featureAtId
608+
const QgsFeatureIds selection = layer->selectedFeaturesIds();
609+
QgsFeatureIds::const_iterator it = selection.constBegin();
610+
for ( ; it != selection.constEnd(); ++it )
611+
{
612+
if ( !layer->featureAtId( *it, currentFeature, true, true ) )
613+
{
614+
continue;
615+
}
616+
map.insert( currentFeature.attributeMap()[ uniqueIdField ].toString(), currentFeature.id() );
617+
}
544618
}
545619
else
546620
{
547-
useField = true;
548621
layer->select( layer->pendingAllAttributesList(), QgsRectangle(), true, false );
549622
while ( layer->nextFeature( currentFeature ) )
550623
{
551-
map.insert( currentFeature.attributeMap()[uniqueIdField].toString(), currentFeature.id() );
624+
map.insert( currentFeature.attributeMap()[ uniqueIdField ].toString(), currentFeature.id() );
552625
}
553626
}
554-
QMultiMap<QString, int>::const_iterator jt;
555-
for (jt = map.constBegin(); jt != map.constEnd(); ++jt)
627+
628+
QgsGeometry* dissolveGeometry; //dissolve geometry
629+
QMultiMap<QString, int>::const_iterator jt = map.constBegin();
630+
QgsFeature outputFeature;
631+
while ( jt != map.constEnd() )
556632
{
557633
QString currentKey = jt.key();
558634
int processedFeatures = 0;
635+
bool first = true;
559636
//take only selection
560637
if ( onlySelectedFeatures )
561638
{
562639
//use QgsVectorLayer::featureAtId
563-
const QgsFeatureIds selection = layer->selectedFeaturesIds();
640+
const QgsFeatureIds selection = layer->selectedFeaturesIds();
564641
if ( p )
565642
{
566-
p->setMaximum( selection.size() );
643+
p->setMaximum( selection.size() );
567644
}
568-
processedFeatures = 0;
569-
while ( jt != map.end() && ( jt.key() == currentKey || !useField ) )
645+
while ( jt != map.constEnd() && ( jt.key() == currentKey || !useField ) )
570646
{
571647
if ( p && p->wasCanceled() )
572648
{
@@ -576,20 +652,22 @@ bool QgsGeometryAnalyzer::dissolve( QgsVectorLayer* layer, const QString& shapef
576652
{
577653
if ( p )
578654
{
579-
p->setValue( processedFeatures );
655+
p->setValue( processedFeatures );
580656
}
581657
if ( !layer->featureAtId( jt.value(), currentFeature, true, true ) )
582658
{
583-
continue;
659+
continue;
660+
}
661+
if ( first )
662+
{
663+
outputFeature.setAttributeMap( currentFeature.attributeMap() );
664+
first = false;
584665
}
585666
dissolveFeature( currentFeature, processedFeatures, &dissolveGeometry );
586667
++processedFeatures;
587668
}
588669
++jt;
589670
}
590-
QgsFeature dissolveFeature;
591-
dissolveFeature.setGeometry( dissolveGeometry );
592-
vWriter.addFeature( dissolveFeature );
593671
}
594672
//take all features
595673
else
@@ -599,8 +677,7 @@ bool QgsGeometryAnalyzer::dissolve( QgsVectorLayer* layer, const QString& shapef
599677
{
600678
p->setMaximum( featureCount );
601679
}
602-
processedFeatures = 0;
603-
while ( jt != map.end() && ( jt.key() == currentKey || !useField ) )
680+
while ( jt != map.constEnd() && ( jt.key() == currentKey || !useField ) )
604681
{
605682
if ( p )
606683
{
@@ -615,22 +692,24 @@ bool QgsGeometryAnalyzer::dissolve( QgsVectorLayer* layer, const QString& shapef
615692
{
616693
continue;
617694
}
695+
{
696+
outputFeature.setAttributeMap( currentFeature.attributeMap() );
697+
first = false;
698+
}
618699
dissolveFeature( currentFeature, processedFeatures, &dissolveGeometry );
619700
++processedFeatures;
620701
++jt;
621702
}
622-
QgsFeature dissolveFeature;
623-
dissolveFeature.setGeometry( dissolveGeometry );
624-
vWriter.addFeature( dissolveFeature );
625703
}
704+
outputFeature.setGeometry( dissolveGeometry );
705+
vWriter.addFeature( outputFeature );
626706
}
627707
return true;
628708
}
629709

630710
void QgsGeometryAnalyzer::dissolveFeature( QgsFeature& f, int nProcessedFeatures, QgsGeometry** dissolveGeometry )
631711
{
632712
QgsGeometry* featureGeometry = f.geometry();
633-
QgsGeometry* tmpGeometry = 0;
634713

635714
if ( !featureGeometry )
636715
{
@@ -639,13 +718,15 @@ void QgsGeometryAnalyzer::dissolveFeature( QgsFeature& f, int nProcessedFeatures
639718

640719
if ( nProcessedFeatures == 0 )
641720
{
642-
*dissolveGeometry = featureGeometry;
721+
int geomSize = featureGeometry->wkbSize();
722+
*dissolveGeometry = new QgsGeometry();
723+
unsigned char* wkb = new unsigned char[geomSize];
724+
memcpy(wkb, featureGeometry->asWkb(), geomSize);
725+
(*dissolveGeometry)->fromWkb(wkb, geomSize);
643726
}
644727
else
645728
{
646-
tmpGeometry = *dissolveGeometry;
647729
*dissolveGeometry = ( *dissolveGeometry )->combine( featureGeometry );
648-
delete tmpGeometry;
649730
}
650731
}
651732

0 commit comments

Comments
 (0)