@@ -83,8 +83,6 @@ namespace pal
83
83
fnIsCancelled = 0 ;
84
84
fnIsCancelledContext = 0 ;
85
85
86
- layers = new QList<Layer*>();
87
-
88
86
ejChainDeg = 50 ;
89
87
tenure = 10 ;
90
88
candListSize = 0.2 ;
@@ -109,88 +107,74 @@ namespace pal
109
107
110
108
}
111
109
112
- QList<Layer*> * Pal::getLayers ()
110
+ QList<Layer*> Pal::getLayers ()
113
111
{
114
112
// TODO make const ! or whatever else
115
- return layers ;
113
+ return mLayers . values () ;
116
114
}
117
115
118
- Layer *Pal::getLayer ( const QString& lyrName )
116
+ Layer *Pal::getLayer ( const QString& layerName )
119
117
{
120
118
mMutex .lock ();
121
- for ( QList<Layer*>::iterator it = layers->begin (); it != layers->end (); ++it )
122
- if (( *it )->name () == lyrName )
123
- {
124
- mMutex .unlock ();
125
- return *it;
126
- }
119
+ if ( !mLayers .contains ( layerName ) )
120
+ {
121
+ mMutex .unlock ();
122
+ throw new PalException::UnknownLayer ();
123
+ }
127
124
125
+ Layer* result = mLayers .value ( layerName );
128
126
mMutex .unlock ();
129
- throw new PalException::UnknownLayer () ;
127
+ return result ;
130
128
}
131
129
132
-
133
130
void Pal::removeLayer ( Layer *layer )
134
131
{
132
+ if ( !layer )
133
+ return ;
134
+
135
135
mMutex .lock ();
136
- if ( layer )
136
+ QString key = mLayers .key ( layer, QString () );
137
+ if ( !key.isEmpty () )
137
138
{
138
- layers->removeOne ( layer );
139
- delete layer;
139
+ mLayers .remove ( key );
140
140
}
141
+ delete layer;
141
142
mMutex .unlock ();
142
143
}
143
144
144
-
145
145
Pal::~Pal ()
146
146
{
147
147
148
148
mMutex .lock ();
149
- while ( layers->size () > 0 )
150
- {
151
- delete layers->front ();
152
- layers->pop_front ();
153
- }
154
149
155
- delete layers;
150
+ qDeleteAll ( mLayers );
151
+ mLayers .clear ();
156
152
mMutex .unlock ();
157
153
158
154
// do not init and exit GEOS - we do it inside QGIS
159
155
// finishGEOS();
160
156
}
161
157
162
-
163
- Layer * Pal::addLayer ( const QString &lyrName, Arrangement arrangement, double defaultPriority, bool obstacle, bool active, bool toLabel, bool displayAll )
158
+ Layer* Pal::addLayer ( const QString &layerName, Arrangement arrangement, double defaultPriority, bool obstacle, bool active, bool toLabel, bool displayAll )
164
159
{
165
- Layer *lyr;
166
160
mMutex .lock ();
167
161
168
- #ifdef _DEBUG_
169
- std::cout << " Pal::addLayer" << std::endl;
170
- std::cout << " lyrName:" << lyrName << std::endl;
171
- std::cout << " nbLayers:" << layers->size () << std::endl;
172
- #endif
173
-
174
- for ( QList<Layer*>::iterator it = layers->begin (); it != layers->end (); ++it )
162
+ // check if layer is already known
163
+ if ( mLayers .contains ( layerName ) )
175
164
{
176
- if (( *it )->name () == lyrName ) // if layer already known
177
- {
178
- mMutex .unlock ();
179
- // There is already a layer with this name, so we just return the existing one.
180
- // Sometimes the same layer is added twice (e.g. datetime split with otf-reprojection)
181
- return *it;
182
- }
165
+ mMutex .unlock ();
166
+ // There is already a layer with this name, so we just return the existing one.
167
+ // Sometimes the same layer is added twice (e.g. datetime split with otf-reprojection)
168
+ return mLayers .value ( layerName );
183
169
}
184
170
185
- lyr = new Layer ( lyrName, arrangement, defaultPriority, obstacle, active, toLabel, this , displayAll );
186
- layers->push_back ( lyr );
187
-
171
+ Layer* layer = new Layer ( layerName, arrangement, defaultPriority, obstacle, active, toLabel, this , displayAll );
172
+ mLayers .insert ( layerName, layer );
188
173
mMutex .unlock ();
189
174
190
- return lyr ;
175
+ return layer ;
191
176
}
192
177
193
-
194
178
typedef struct _featCbackCtx
195
179
{
196
180
Layer *layer;
@@ -302,7 +286,7 @@ namespace pal
302
286
return true ;
303
287
}
304
288
305
- Problem* Pal::extract ( int nbLayers, const QStringList& layersName , double lambda_min, double phi_min, double lambda_max, double phi_max )
289
+ Problem* Pal::extract ( const QStringList& layerNames , double lambda_min, double phi_min, double lambda_max, double phi_max )
306
290
{
307
291
// to store obstacles
308
292
RTree<FeaturePart*, double , 2 , double > *obstacles = new RTree<FeaturePart*, double , 2 , double >();
@@ -341,75 +325,50 @@ namespace pal
341
325
context->bbox_max [0 ] = amax[0 ];
342
326
context->bbox_max [1 ] = amax[1 ];
343
327
344
- #ifdef _VERBOSE_
345
- std::cout << nbLayers << " /" << layers->size () << " layers to extract " << std::endl;
346
- #endif
347
-
328
+ // first step : extract features from layers
348
329
349
- /* First step : extract feature from layers
350
- *
351
- * */
352
- int oldNbft = 0 ;
330
+ int previousFeatureCount = 0 ;
353
331
Layer *layer;
354
332
355
- QStringList labLayers ;
333
+ QStringList layersWithFeaturesInBBox ;
356
334
357
335
mMutex .lock ();
358
- for ( i = 0 ; i < nbLayers; i++ )
336
+ Q_FOREACH ( QString layerName, layerNames )
359
337
{
360
- for ( QList<Layer*>::iterator it = layers->begin (); it != layers->end (); ++it ) // iterate on pal->layers
338
+ layer = mLayers .value ( layerName, 0 );
339
+ if ( !layer )
361
340
{
362
- layer = *it;
363
- // Only select those who are active and labellable or those who are active and which must be treated as obstaclewhich must be treated as obstacle
364
- if ( layer->active ()
365
- && ( layer->obstacle () || layer->labelLayer () ) )
366
- {
367
-
368
- // check if this selected layers has been selected by user
369
- if ( layersName.at ( i ) == layer->name () )
370
- {
371
- // check for connected features with the same label text and join them
372
- if ( layer->mergeConnectedLines () )
373
- layer->joinConnectedFeatures ();
374
-
375
- layer->chopFeaturesAtRepeatDistance ();
376
-
341
+ // invalid layer name
342
+ continue ;
343
+ }
377
344
378
- context->layer = layer;
379
- // lookup for feature (and generates candidates list)
345
+ // only select those who are active
346
+ if ( !layer->active () )
347
+ continue ;
380
348
381
- context-> layer -> mMutex . lock ();
382
- context-> layer ->rtree -> Search ( amin, amax, extractFeatCallback, ( void * ) context );
383
- context-> layer ->mMutex . unlock ();
349
+ // check for connected features with the same label text and join them
350
+ if ( layer->mergeConnectedLines () )
351
+ layer->joinConnectedFeatures ();
384
352
385
- #ifdef _VERBOSE_
386
- std::cout << " Layer's name: " << layer->getName () << std::endl;
387
- std::cout << " active:" << layer->isToLabel () << std::endl;
388
- std::cout << " obstacle:" << layer->isObstacle () << std::endl;
389
- std::cout << " toLabel:" << layer->isToLabel () << std::endl;
390
- std::cout << " # features: " << layer->getNbFeatures () << std::endl;
391
- std::cout << " # extracted features: " << context->fFeats ->size () - oldNbft << std::endl;
392
- #endif
393
- if ( context->fFeats ->size () - oldNbft > 0 )
394
- {
395
- labLayers << layer->name ();
396
- }
397
- oldNbft = context->fFeats ->size ();
353
+ layer->chopFeaturesAtRepeatDistance ();
398
354
355
+ // find features within bounding box and generate candidates list
356
+ context->layer = layer;
357
+ context->layer ->mMutex .lock ();
358
+ context->layer ->rtree ->Search ( amin, amax, extractFeatCallback, ( void * ) context );
359
+ context->layer ->mMutex .unlock ();
399
360
400
- break ;
401
- }
402
- }
361
+ if ( context-> fFeats -> size () - previousFeatureCount > 0 )
362
+ {
363
+ layersWithFeaturesInBBox << layer-> name ();
403
364
}
365
+ previousFeatureCount = context->fFeats ->size ();
404
366
}
405
367
delete context;
406
368
mMutex .unlock ();
407
369
408
- prob->nbLabelledLayers = labLayers.size ();
409
- for ( i = 0 ; i < prob->nbLabelledLayers ; i++ )
410
- {
411
- prob->labelledLayersName << labLayers.takeFirst ();
412
- }
370
+ prob->nbLabelledLayers = layersWithFeaturesInBBox.size ();
371
+ prob->labelledLayersName = layersWithFeaturesInBBox;
413
372
414
373
if ( fFeats ->size () == 0 )
415
374
{
@@ -577,36 +536,13 @@ namespace pal
577
536
578
537
std::list<LabelPosition*>* Pal::labeller ( double bbox[4 ], PalStat **stats, bool displayAll )
579
538
{
580
-
581
- #ifdef _DEBUG_FULL_
582
- std::cout << " LABELLER (active)" << std::endl;
583
- #endif
584
- int i;
585
-
586
- mMutex .lock ();
587
- int nbLayers = layers->size ();
588
-
589
- QStringList layersName;
590
- Layer *layer;
591
- i = 0 ;
592
- for ( QList<Layer*>::iterator it = layers->begin (); it != layers->end (); ++it )
593
- {
594
- layer = *it;
595
- layersName << layer->name ();
596
- i++;
597
- }
598
- mMutex .unlock ();
599
-
600
- std::list<LabelPosition*> * solution = labeller ( nbLayers, layersName, bbox, stats, displayAll );
601
-
602
- return solution;
539
+ return labeller ( mLayers .keys (), bbox, stats, displayAll );
603
540
}
604
541
605
-
606
542
/*
607
543
* BIG MACHINE
608
544
*/
609
- std::list<LabelPosition*>* Pal::labeller ( int nbLayers, const QStringList& layersName , double bbox[4 ], PalStat **stats, bool displayAll )
545
+ std::list<LabelPosition*>* Pal::labeller ( const QStringList& layerNames , double bbox[4 ], PalStat **stats, bool displayAll )
610
546
{
611
547
#ifdef _DEBUG_
612
548
std::cout << " LABELLER (selection)" << std::endl;
@@ -631,7 +567,7 @@ namespace pal
631
567
t.start ();
632
568
633
569
// First, extract the problem
634
- if (( prob = extract ( nbLayers, layersName , bbox[0 ], bbox[1 ], bbox[2 ], bbox[3 ] ) ) == NULL )
570
+ if (( prob = extract ( layerNames , bbox[0 ], bbox[1 ], bbox[2 ], bbox[3 ] ) ) == NULL )
635
571
{
636
572
// nothing to be done => return an empty result set
637
573
if ( stats )
@@ -706,24 +642,7 @@ namespace pal
706
642
707
643
Problem* Pal::extractProblem ( double bbox[4 ] )
708
644
{
709
- // find out: nbLayers, layersName, layersFactor
710
- mMutex .lock ();
711
- int nbLayers = layers->size ();
712
-
713
- QStringList layersName;
714
- Layer *layer;
715
- int i = 0 ;
716
- for ( QList<Layer*>::iterator it = layers->begin (); it != layers->end (); ++it )
717
- {
718
- layer = *it;
719
- layersName << layer->name ();
720
- i++;
721
- }
722
- mMutex .unlock ();
723
-
724
- Problem* prob = extract ( nbLayers, layersName, bbox[0 ], bbox[1 ], bbox[2 ], bbox[3 ] );
725
-
726
- return prob;
645
+ return extract ( mLayers .keys (), bbox[0 ], bbox[1 ], bbox[2 ], bbox[3 ] );
727
646
}
728
647
729
648
std::list<LabelPosition*>* Pal::solveProblem ( Problem* prob, bool displayAll )
0 commit comments