forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogrdissolve.cpp
1221 lines (1093 loc) · 45.3 KB
/
ogrdissolve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Allow a user to dissolve geometries based on an attribute.
* Author: Howard Butler, [email protected]
*
******************************************************************************
* Copyright (c) 2007, Howard Butler
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "ogrsf_frmts.h"
#include "ogr_p.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_api.h"
#include "commonutils.h"
#include <map>
#include <list>
static void Usage();
static int DissolveLayer(OGRDataSource *poSrcDS, OGRLayer *poSrcLayer,
OGRDataSource *poDstDS, char **papszLSCO,
const char *pszNewLayerName, int bTransform,
OGRSpatialReference *poOutputSRS,
OGRSpatialReference *poSourceSRS,
char **papszSelFields, int bAppend, int eGType,
int bOverwrite);
static int bSkipFailures = FALSE;
static int nGroupTransactions = 200;
static int bPreserveFID = FALSE;
static int nFIDToFetch = OGRNullFID;
typedef std::multimap<CPLString, OGRGeometry *> StringGeometryMMap;
typedef std::map<CPLString, OGRGeometryCollection *> StringGeometryColMap;
typedef std::map<CPLString, OGRGeometry *> StringGeometryMap;
typedef std::list<OGRGeometry *> GeometriesList;
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(nArgc, papszArgv)
{
const char *pszFormat = "ESRI Shapefile";
const char *pszDataSource = NULL;
const char *pszDestDataSource = NULL;
char **papszLayers = NULL;
char **papszDSCO = NULL, **papszLCO = NULL;
int bTransform = FALSE;
int bAppend = FALSE, bUpdate = FALSE, bOverwrite = FALSE;
const char *pszOutputSRSDef = NULL;
const char *pszSourceSRSDef = NULL;
OGRSpatialReference *poOutputSRS = NULL;
OGRSpatialReference *poSourceSRS = NULL;
const char *pszNewLayerName = NULL;
const char *pszWHERE = NULL;
OGRGeometry *poSpatialFilter = NULL;
const char *pszSelect;
char **papszSelFields = NULL;
const char *pszSQLStatement = NULL;
int eGType = -2;
/* -------------------------------------------------------------------- */
/* Register format(s). */
/* -------------------------------------------------------------------- */
OGRRegisterAll();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
nArgc = OGRGeneralCmdLineProcessor(nArgc, &papszArgv, 0);
if (nArgc < 1)
exit(-nArgc);
for (int iArg = 1; iArg < nArgc; iArg++)
{
if ((EQUAL(papszArgv[iArg], "-f") || EQUAL(papszArgv[iArg], "-of")) &&
iArg < nArgc - 1)
{
pszFormat = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-dsco") && iArg < nArgc - 1)
{
papszDSCO = CSLAddString(papszDSCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-lco") && iArg < nArgc - 1)
{
papszLCO = CSLAddString(papszLCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-preserve_fid"))
{
bPreserveFID = TRUE;
}
else if (STARTS_WITH_CI(papszArgv[iArg], "-skip"))
{
bSkipFailures = TRUE;
}
else if (EQUAL(papszArgv[iArg], "-append"))
{
bAppend = TRUE;
}
else if (EQUAL(papszArgv[iArg], "-overwrite"))
{
bOverwrite = TRUE;
}
else if (EQUAL(papszArgv[iArg], "-update"))
{
bUpdate = TRUE;
}
else if (EQUAL(papszArgv[iArg], "-fid") && papszArgv[iArg + 1] != NULL)
{
nFIDToFetch = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-sql") && papszArgv[iArg + 1] != NULL)
{
pszSQLStatement = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-nln") && iArg < nArgc - 1)
{
pszNewLayerName = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-nlt") && iArg < nArgc - 1)
{
int bIs3D = FALSE;
CPLString osGeomName = papszArgv[iArg + 1];
if (strlen(papszArgv[iArg + 1]) > 3 &&
STARTS_WITH_CI(papszArgv[iArg + 1] +
strlen(papszArgv[iArg + 1]) - 3,
"25D"))
{
bIs3D = TRUE;
osGeomName.resize(osGeomName.size() - 3);
}
else if (strlen(papszArgv[iArg + 1]) > 1 &&
STARTS_WITH_CI(papszArgv[iArg + 1] +
strlen(papszArgv[iArg + 1]) - 1,
"Z"))
{
bIs3D = TRUE;
osGeomName.resize(osGeomName.size() - 1);
}
if (EQUAL(osGeomName, "NONE"))
eGType = wkbNone;
else if (EQUAL(osGeomName, "GEOMETRY"))
eGType = wkbUnknown;
else
{
eGType = OGRFromOGCGeomType(osGeomName);
if (eGType == wkbUnknown)
{
fprintf(stderr, "-nlt %s: type not recognised.\n",
papszArgv[iArg + 1]);
exit(1);
}
}
if (eGType != wkbNone && bIs3D)
eGType = wkbSetZ((OGRwkbGeometryType)eGType);
iArg++;
}
else if (EQUAL(papszArgv[iArg], "-tg") && iArg < nArgc - 1)
{
nGroupTransactions = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-s_srs") && iArg < nArgc - 1)
{
pszSourceSRSDef = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-a_srs") && iArg < nArgc - 1)
{
pszOutputSRSDef = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-t_srs") && iArg < nArgc - 1)
{
pszOutputSRSDef = papszArgv[++iArg];
bTransform = TRUE;
}
else if (EQUAL(papszArgv[iArg], "-spat") &&
papszArgv[iArg + 1] != NULL && papszArgv[iArg + 2] != NULL &&
papszArgv[iArg + 3] != NULL && papszArgv[iArg + 4] != NULL)
{
OGRLinearRing oRing;
oRing.addPoint(CPLAtof(papszArgv[iArg + 1]),
CPLAtof(papszArgv[iArg + 2]));
oRing.addPoint(CPLAtof(papszArgv[iArg + 1]),
CPLAtof(papszArgv[iArg + 4]));
oRing.addPoint(CPLAtof(papszArgv[iArg + 3]),
CPLAtof(papszArgv[iArg + 4]));
oRing.addPoint(CPLAtof(papszArgv[iArg + 3]),
CPLAtof(papszArgv[iArg + 2]));
oRing.addPoint(CPLAtof(papszArgv[iArg + 1]),
CPLAtof(papszArgv[iArg + 2]));
poSpatialFilter = new OGRPolygon();
poSpatialFilter->toPolygon()->addRing(&oRing);
iArg += 4;
}
else if (EQUAL(papszArgv[iArg], "-where") &&
papszArgv[iArg + 1] != NULL)
{
pszWHERE = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-select") &&
papszArgv[iArg + 1] != NULL)
{
pszSelect = papszArgv[++iArg];
papszSelFields =
CSLTokenizeStringComplex(pszSelect, " ,", FALSE, FALSE);
}
else if (papszArgv[iArg][0] == '-')
{
Usage();
}
else if (pszDestDataSource == NULL)
pszDestDataSource = papszArgv[iArg];
else if (pszDataSource == NULL)
pszDataSource = papszArgv[iArg];
else
papszLayers = CSLAddString(papszLayers, papszArgv[iArg]);
}
if (pszDataSource == NULL)
Usage();
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
OGRDataSource *poDS;
poDS = OGRSFDriverRegistrar::Open(pszDataSource, FALSE);
/* -------------------------------------------------------------------- */
/* Report failure */
/* -------------------------------------------------------------------- */
if (poDS == NULL)
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
printf("FAILURE:\n"
"Unable to open datasource `%s' with the following drivers.\n",
pszDataSource);
for (int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++)
{
printf(" -> %s\n", poR->GetDriver(iDriver)->GetName());
}
exit(1);
}
/* -------------------------------------------------------------------- */
/* Try opening the output datasource as an existing, writable */
/* -------------------------------------------------------------------- */
OGRDataSource *poODS;
if (bUpdate)
{
poODS = OGRSFDriverRegistrar::Open(pszDestDataSource, TRUE);
if (poODS == NULL)
{
printf("FAILURE:\n"
"Unable to open existing output datasource `%s'.\n",
pszDestDataSource);
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Find the output driver. */
/* -------------------------------------------------------------------- */
else
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
OGRSFDriver *poDriver = NULL;
int iDriver;
for (iDriver = 0; iDriver < poR->GetDriverCount() && poDriver == NULL;
iDriver++)
{
if (EQUAL(poR->GetDriver(iDriver)->GetName(), pszFormat))
{
poDriver = poR->GetDriver(iDriver);
}
}
if (poDriver == NULL)
{
printf("Unable to find driver `%s'.\n", pszFormat);
printf("The following drivers are available:\n");
for (iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++)
{
printf(" -> `%s'\n", poR->GetDriver(iDriver)->GetName());
}
exit(1);
}
if (!poDriver->TestCapability(ODrCCreateDataSource))
{
printf("%s driver does not support data source creation.\n",
pszFormat);
exit(1);
}
/* --------------------------------------------------------------------
*/
/* Create the output data source. */
/* --------------------------------------------------------------------
*/
poODS = poDriver->CreateDataSource(pszDestDataSource, papszDSCO);
if (poODS == NULL)
{
printf("%s driver failed to create %s\n", pszFormat,
pszDestDataSource);
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Parse the output SRS definition if possible. */
/* -------------------------------------------------------------------- */
if (pszOutputSRSDef != NULL)
{
poOutputSRS = new OGRSpatialReference();
poOutputSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
if (poOutputSRS->SetFromUserInput(pszOutputSRSDef) != OGRERR_NONE)
{
printf("Failed to process SRS definition: %s\n", pszOutputSRSDef);
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Parse the source SRS definition if possible. */
/* -------------------------------------------------------------------- */
if (pszSourceSRSDef != NULL)
{
poSourceSRS = new OGRSpatialReference();
poSourceSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
if (poSourceSRS->SetFromUserInput(pszSourceSRSDef) != OGRERR_NONE)
{
printf("Failed to process SRS definition: %s\n", pszSourceSRSDef);
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Special case for -sql clause. No source layers required. */
/* -------------------------------------------------------------------- */
if (pszSQLStatement != NULL)
{
OGRLayer *poResultSet;
if (pszWHERE != NULL)
printf("-where clause ignored in combination with -sql.\n");
if (CSLCount(papszLayers) > 0)
printf("layer names ignored in combination with -sql.\n");
poResultSet = poDS->ExecuteSQL(pszSQLStatement, poSpatialFilter, NULL);
if (poResultSet != NULL)
{
if (!DissolveLayer(poDS, poResultSet, poODS, papszLCO,
pszNewLayerName, bTransform, poOutputSRS,
poSourceSRS, papszSelFields, bAppend, eGType,
bOverwrite))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Terminating translation prematurely after failed\n"
"translation from sql statement.");
exit(1);
}
poDS->ReleaseResultSet(poResultSet);
}
}
/* -------------------------------------------------------------------- */
/* Process each data source layer. */
/* -------------------------------------------------------------------- */
for (int iLayer = 0;
pszSQLStatement == NULL && iLayer < poDS->GetLayerCount(); iLayer++)
{
OGRLayer *poLayer = poDS->GetLayer(iLayer);
if (poLayer == NULL)
{
printf("FAILURE: Couldn't fetch advertised layer %d!\n", iLayer);
exit(1);
}
if (CSLCount(papszLayers) == 0 ||
CSLFindString(papszLayers, poLayer->GetLayerDefn()->GetName()) !=
-1)
{
if (pszWHERE != NULL)
poLayer->SetAttributeFilter(pszWHERE);
if (poSpatialFilter != NULL)
poLayer->SetSpatialFilter(poSpatialFilter);
if (!DissolveLayer(poDS, poLayer, poODS, papszLCO, pszNewLayerName,
bTransform, poOutputSRS, poSourceSRS,
papszSelFields, bAppend, eGType, bOverwrite) &&
!bSkipFailures)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Terminating translation prematurely after failed\n"
"translation of layer %s\n",
poLayer->GetLayerDefn()->GetName());
exit(1);
}
}
}
/* -------------------------------------------------------------------- */
/* Close down. */
/* -------------------------------------------------------------------- */
OGRSpatialReference::DestroySpatialReference(poOutputSRS);
OGRSpatialReference::DestroySpatialReference(poSourceSRS);
OGRDataSource::DestroyDataSource(poODS);
OGRDataSource::DestroyDataSource(poDS);
CSLDestroy(papszSelFields);
CSLDestroy(papszArgv);
CSLDestroy(papszLayers);
CSLDestroy(papszDSCO);
CSLDestroy(papszLCO);
OGRCleanupAll();
#ifdef DBMALLOC
malloc_dump(1);
#endif
return 0;
}
MAIN_END
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
printf(
"Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update]\n"
" [-select field_list] [-where restricted_where] \n"
" [-sql <sql statement>] \n"
" [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid "
"FID]\n"
" [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n"
" [-f format_name] [-overwrite] [[-dsco NAME=VALUE] "
"...]\n"
" dst_datasource_name src_datasource_name\n"
" [-lco NAME=VALUE] [-nln name] [-nlt type] [layer "
"[layer ...]]\n"
"\n"
" -f format_name: output file format name, possible values are:\n");
for (int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++)
{
OGRSFDriver *poDriver = poR->GetDriver(iDriver);
if (poDriver->TestCapability(ODrCCreateDataSource))
printf(" -f \"%s\"\n", poDriver->GetName());
}
printf(
" -append: Append to existing layer instead of creating new if it "
"exists\n"
" -overwrite: delete the output layer and recreate it empty\n"
" -update: Open existing output datasource in update mode\n"
" -select field_list: Comma-delimited list of fields from input layer "
"to\n"
" copy to the new layer (defaults to all)\n"
" -where restricted_where: Attribute query (like SQL WHERE)\n"
" -sql statement: Execute given SQL statement and save result.\n"
" -skipfailures: skip features or layers that fail to convert\n"
" -spat xmin ymin xmax ymax: spatial query extents\n"
" -dsco NAME=VALUE: Dataset creation option (format specific)\n"
" -lco NAME=VALUE: Layer creation option (format specific)\n"
" -nln name: Assign an alternate name to the new layer\n"
" -nlt type: Force a geometry type for new layer. One of NONE, "
"GEOMETRY,\n"
" POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, "
"MULTILINE,\n"
" MULTIPOLYGON, or MULTILINESTRING. Add \"25D\" for 3D layers.\n"
" Default is type of source layer.\n");
printf(" -a_srs srs_def: Assign an output SRS\n"
" -t_srs srs_def: Reproject/transform to this SRS on output\n"
" -s_srs srs_def: Override source SRS\n"
"\n"
" Srs_def can be a full WKT definition (hard to escape properly),\n"
" or a well known definition (i.e. EPSG:4326) or a file with a WKT\n"
" definition.\n");
exit(1);
}
StringGeometryMap *CollectGeometries(OGRLayer *poSrcLayer,
const char **papszFields)
{
/* -------------------------------------------------------------------- */
/* CollectGeometries returns a dictionary where the keys are the */
/* values in the fields that the user has selected and the values */
/* are a GeometryCollection of all of the geometries for records */
/* with that value. */
/* -------------------------------------------------------------------- */
StringGeometryMMap poGeometriesMap;
poSrcLayer->ResetReading();
int iField;
/* -------------------------------------------------------------------- */
/* Get all of the features and put them in a multi map. This may */
/* include values for which the selected fields is NULL. */
/* -------------------------------------------------------------------- */
for (auto &poFeature : poSrcLayer)
{
CPLString poKey("");
for (iField = 0; papszFields[iField] != NULL; iField++)
{
int nField = poFeature->GetFieldIndex(papszFields[iField]);
poKey = poKey + poFeature->GetFieldAsString(nField);
}
if (poFeature->GetGeometryRef()->IsValid())
{
poGeometriesMap.insert(
std::make_pair(CPLString(poKey), poFeature->GetGeometryRef()));
}
else
{
CPLDebug("CollectGeometries",
"Geometry was invalid not adding!!!!");
}
}
/* -------------------------------------------------------------------- */
/* Loop through our features map and get a unique list of field */
/* values. This could be done using something other than a map */
/* of course, but it was convenient. */
/* -------------------------------------------------------------------- */
typedef std::map<CPLString, int> StringIntMap;
StringIntMap::const_iterator ipos;
StringIntMap poFieldsmap;
StringGeometryMMap::const_iterator pos;
for (pos = poGeometriesMap.begin(); pos != poGeometriesMap.end(); ++pos)
{
/* we currently throw out any null field values at this time */
// if (!(pos->first.empty())) {
poFieldsmap[CPLString(pos->first.c_str())] = 1;
// }
}
/* -------------------------------------------------------------------- */
/* Make a new map of GeometryCollection for each value in the */
/* poFieldsmap. This is a 1:1 relationship, and all of the */
/* geometries for a given field are all put into the same */
/* GeometryCollection. After we build the poCollections, we will */
/* use GEOS' buffer(0) trick to have GEOS perform the segmentation */
/* -------------------------------------------------------------------- */
StringGeometryColMap poCollections;
CPLDebug("CollectGeometries", "Field map size: %d", poFieldsmap.size());
for (ipos = poFieldsmap.begin(); ipos != poFieldsmap.end(); ++ipos)
{
CPLString fid = ipos->first;
CPLDebug("CollectGeometries", "First %s Second %d", ipos->first.c_str(),
ipos->second);
OGRGeometryCollection *geom = new OGRGeometryCollection;
for (pos = poGeometriesMap.lower_bound(fid);
pos != poGeometriesMap.upper_bound(fid); ++pos)
{
geom->addGeometry(pos->second);
}
poCollections.insert(std::make_pair(fid, geom));
}
CPLDebug("CollectGeometries", "Geo map size: %d", poCollections.size());
/* -------------------------------------------------------------------- */
/* Loop through our poCollections map and buffer(0) each */
/* GeometryCollection. GEOS will collapse the geometries down */
/* -------------------------------------------------------------------- */
StringGeometryMap *buffers = new StringGeometryMap;
StringGeometryColMap::const_iterator collections_i;
for (collections_i = poCollections.begin();
collections_i != poCollections.end(); ++collections_i)
{
CPLDebug("CollectGeometries", "poCollections Geometry size %d",
collections_i->second->getNumGeometries());
OGRGeometry *buffer = collections_i->second->Buffer(0);
buffers->insert(std::make_pair(collections_i->first, buffer));
}
for (collections_i = poCollections.begin();
collections_i != poCollections.end(); ++collections_i)
{
delete collections_i->second;
}
return buffers;
}
GeometriesList *FlattenGeometries(GeometriesList *input)
{
GeometriesList::const_iterator geometry_i;
GeometriesList *output = new GeometriesList;
CPLDebug("CollectGeometries",
"Input geometries in FlattenGeometries size: %d", input->size());
for (geometry_i = input->begin(); geometry_i != input->end(); ++geometry_i)
{
OGRGeometry *buffer = (*geometry_i);
// int nGeometries = buffer->getNumGeometries();
OGRwkbGeometryType iGType = buffer->getGeometryType();
if (iGType == wkbPolygon)
{
output->push_back(buffer);
CPLDebug("CollectGeometries",
"Collapsing wkbPolygon geometries......");
}
if (iGType == wkbMultiPolygon)
{
OGRMultiPolygon *geom = buffer->toMultiPolygon();
for (int i = 0; i < geom->getNumGeometries(); i++)
{
OGRPolygon *g = geom->getGeometryRef(i)->toPolygon();
output->push_back((OGRGeometry *)g);
}
CPLDebug("CollectGeometries",
"Collapsing wkbMultiPolygon geometries......");
}
if (iGType == wkbGeometryCollection)
{
OGRGeometryCollection *geom = buffer->toGeometryCollection();
GeometriesList *collection = new GeometriesList;
GeometriesList::const_iterator g_i;
for (int i = 0; i < geom->getNumGeometries(); i++)
{
OGRGeometry *g = geom->getGeometryRef(i);
collection->push_back(g);
}
GeometriesList *collapsed = FlattenGeometries(collection);
for (g_i = collapsed->begin(); g_i != collapsed->end(); g_i++)
{
output->push_back((OGRGeometry *)(*g_i));
CPLDebug("CollectGeometries",
"Collapsing wkbGeometryCollection geometries.");
}
}
// CPLDebug( "CollectGeometries",
// "Buffered Geometry size %d",
// nGeometries);
}
return output;
}
/************************************************************************/
/* DissolveLayer() */
/************************************************************************/
static int DissolveLayer(OGRDataSource *poSrcDS, OGRLayer *poSrcLayer,
OGRDataSource *poDstDS, char **papszLCO,
const char *pszNewLayerName, int bTransform,
OGRSpatialReference *poOutputSRS,
OGRSpatialReference *poSourceSRS,
char **papszSelFields, int bAppend, int eGType,
int bOverwrite)
{
OGRLayer *poDstLayer;
OGRFeatureDefn *poFDefn;
OGRErr eErr;
int bForceToPolygon = FALSE;
int bForceToMultiPolygon = FALSE;
if (pszNewLayerName == NULL)
pszNewLayerName = poSrcLayer->GetLayerDefn()->GetName();
if (wkbFlatten(eGType) == wkbPolygon)
bForceToPolygon = TRUE;
else if (wkbFlatten(eGType) == wkbMultiPolygon)
bForceToMultiPolygon = TRUE;
/* -------------------------------------------------------------------- */
/* Setup coordinate transformation if we need it. */
/* -------------------------------------------------------------------- */
OGRCoordinateTransformation *poCT = NULL;
if (bTransform)
{
if (poSourceSRS == NULL)
poSourceSRS = poSrcLayer->GetSpatialRef();
if (poSourceSRS == NULL)
{
printf("Can't transform coordinates, source layer has no\n"
"coordinate system. Use -s_srs to set one.\n");
exit(1);
}
CPLAssert(NULL != poSourceSRS);
CPLAssert(NULL != poOutputSRS);
poCT = OGRCreateCoordinateTransformation(poSourceSRS, poOutputSRS);
if (poCT == NULL)
{
char *pszWKT = NULL;
printf("Failed to create coordinate transformation between the\n"
"following coordinate systems. This may be because they\n"
"are not transformable, or because projection services\n"
"(PROJ.4 DLL/.so) could not be loaded.\n");
poSourceSRS->exportToPrettyWkt(&pszWKT, FALSE);
printf("Source:\n%s\n", pszWKT);
poOutputSRS->exportToPrettyWkt(&pszWKT, FALSE);
printf("Target:\n%s\n", pszWKT);
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Get other info. */
/* -------------------------------------------------------------------- */
poFDefn = poSrcLayer->GetLayerDefn();
if (poOutputSRS == NULL)
poOutputSRS = poSrcLayer->GetSpatialRef();
/* -------------------------------------------------------------------- */
/* Find the layer. */
/* -------------------------------------------------------------------- */
int iLayer = -1;
poDstLayer = NULL;
for (iLayer = 0; iLayer < poDstDS->GetLayerCount(); iLayer++)
{
OGRLayer *poLayer = poDstDS->GetLayer(iLayer);
if (poLayer != NULL &&
EQUAL(poLayer->GetLayerDefn()->GetName(), pszNewLayerName))
{
poDstLayer = poLayer;
break;
}
}
/* -------------------------------------------------------------------- */
/* If the user requested overwrite, and we have the layer in */
/* question we need to delete it now so it will get recreated */
/* (overwritten). */
/* -------------------------------------------------------------------- */
if (poDstLayer != NULL && bOverwrite)
{
if (poDstDS->DeleteLayer(iLayer) != OGRERR_NONE)
{
fprintf(stderr, "DeleteLayer() failed when overwrite requested.\n");
return FALSE;
}
poDstLayer = NULL;
}
/* -------------------------------------------------------------------- */
/* If the layer does not exist, then create it. */
/* -------------------------------------------------------------------- */
if (poDstLayer == NULL)
{
if (eGType == -2)
eGType = poFDefn->GetGeomType();
if (!poDstDS->TestCapability(ODsCCreateLayer))
{
fprintf(
stderr,
"Layer %s not found, and CreateLayer not supported by driver.",
pszNewLayerName);
return FALSE;
}
CPLErrorReset();
poDstLayer = poDstDS->CreateLayer(pszNewLayerName, poOutputSRS,
(OGRwkbGeometryType)eGType, papszLCO);
if (poDstLayer == NULL)
return FALSE;
bAppend = FALSE;
}
/* -------------------------------------------------------------------- */
/* Otherwise we will append to it, if append was requested. */
/* -------------------------------------------------------------------- */
else if (!bAppend)
{
printf("FAILED: Layer %s already exists, and -append not specified.\n"
" Consider using -append, or -overwrite.\n",
pszNewLayerName);
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Add fields. Default to copy all field. */
/* If only a subset of all fields requested, then output only */
/* the selected fields, and in the order that they were */
/* selected. */
/* -------------------------------------------------------------------- */
int iField;
if (papszSelFields && !bAppend)
{
for (iField = 0; papszSelFields[iField] != NULL; iField++)
{
int iSrcField = poFDefn->GetFieldIndex(papszSelFields[iField]);
if (iSrcField >= 0)
poDstLayer->CreateField(poFDefn->GetFieldDefn(iSrcField));
else
{
printf("Field '%s' not found in source layer.\n",
papszSelFields[iField]);
if (!bSkipFailures)
return FALSE;
}
}
}
else if (!bAppend)
{
for (iField = 0; iField < poFDefn->GetFieldCount(); iField++)
poDstLayer->CreateField(poFDefn->GetFieldDefn(iField));
}
/* -------------------------------------------------------------------- */
/* Transfer features. */
/* -------------------------------------------------------------------- */
OGRFeature *poFeature;
int nFeaturesInTransaction = 0;
poSrcLayer->ResetReading();
if (nGroupTransactions)
poDstLayer->StartTransaction();
StringGeometryMap *buffers =
CollectGeometries(poSrcLayer, (const char **)papszSelFields);
StringGeometryMap::const_iterator buffers_i;
GeometriesList *input = new GeometriesList;
CPLDebug("CollectGeometries", "Buffers size: %d", buffers->size());
for (buffers_i = buffers->begin(); buffers_i != buffers->end(); ++buffers_i)
{
input->push_back(buffers_i->second);
}
GeometriesList *geometries = FlattenGeometries(input);
GeometriesList::const_iterator g_i;
for (g_i = geometries->begin(); g_i != geometries->end(); g_i++)
{
OGRFeature *feature = new OGRFeature(poFDefn);
feature->SetGeometry((*g_i));
feature->SetField("TAXDIST", "fid");
poDstLayer->CreateFeature(feature);
}
if (nGroupTransactions)
poDstLayer->CommitTransaction();
// getGeometryType
// if( pszNewLayerName == NULL )
// pszNewLayerName = poSrcLayer->GetLayerDefn()->GetName();
//
// if( wkbFlatten(eGType) == wkbPolygon )
// bForceToPolygon = TRUE;
// else if( wkbFlatten(eGType) == wkbMultiPolygon )
// bForceToMultiPolygon = TRUE;
//
// /* --------------------------------------------------------------------
// */
// /* Setup coordinate transformation if we need it. */
// /* --------------------------------------------------------------------
// */
// OGRCoordinateTransformation *poCT = NULL;
//
// if( bTransform )
// {
// if( poSourceSRS == NULL )
// poSourceSRS = poSrcLayer->GetSpatialRef();
//
// if( poSourceSRS == NULL )
// {
// printf( "Can't transform coordinates, source layer has no\n"
// "coordinate system. Use -s_srs to set one.\n" );
// exit( 1 );
// }
//
// CPLAssert( NULL != poSourceSRS );
// CPLAssert( NULL != poOutputSRS );
//
// poCT = OGRCreateCoordinateTransformation( poSourceSRS,
// poOutputSRS ); if( poCT == NULL )
// {
// char *pszWKT = NULL;
//
// printf("Failed to create coordinate transformation between
// the\n"
// "following coordinate systems. This may be because
// they\n" "are not transformable, or because projection
// services\n"
// "(PROJ.4 DLL/.so) could not be loaded.\n" );
//
// poSourceSRS->exportToPrettyWkt( &pszWKT, FALSE );
// printf( "Source:\n%s\n", pszWKT );
//
// poOutputSRS->exportToPrettyWkt( &pszWKT, FALSE );
// printf( "Target:\n%s\n", pszWKT );
// exit( 1 );
// }
// }
//
// /* --------------------------------------------------------------------
// */
// /* Get other info. */
// /* --------------------------------------------------------------------
// */
// poFDefn = poSrcLayer->GetLayerDefn();
//
// if( poOutputSRS == NULL )
// poOutputSRS = poSrcLayer->GetSpatialRef();
//
// /* --------------------------------------------------------------------
// */
// /* Find the layer. */
// /* --------------------------------------------------------------------
// */
// int iLayer = -1;
// poDstLayer = NULL;
//
// for( iLayer = 0; iLayer < poDstDS->GetLayerCount(); iLayer++ )
// {
// OGRLayer *poLayer = poDstDS->GetLayer(iLayer);
//
// if( poLayer != NULL
// && EQUAL(poLayer->GetLayerDefn()->GetName(),pszNewLayerName)
// )
// {
// poDstLayer = poLayer;
// break;