forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnmanalyse.cpp
650 lines (569 loc) · 22.8 KB
/
gnmanalyse.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
/******************************************************************************
* Project: geography network utility
* Purpose: Analyse GNM networks
* Authors: Mikhail Gusev, gusevmihs at gmail dot com
* Dmitry Baryshnikov, [email protected]
*
******************************************************************************
* Copyright (C) 2014 Mikhail Gusev
* Copyright (c) 2014-2015, NextGIS <[email protected]>
*
* 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 "commonutils.h"
#include "gdal_version.h"
#include "gnm.h"
#include "gnm_priv.h"
#include "ogr_p.h"
enum operation
{
op_unknown = 0, /** no operation */
op_dijkstra, /** create shortest path using Dijkstra algorithm */
op_kpaths, /** create k shortest paths using Yens algorithm */
op_resource /** create resource distribution layer */
};
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage(bool bIsError, const char *pszAdditionalMsg = nullptr,
bool bShort = true)
{
fprintf(
bIsError ? stderr : stdout,
"Usage: gnmanalyse [--help][--help-general][-q][-quiet][--long-usage]\n"
" [dijkstra <start_gfid> <end_gfid "
"[-alo <NAME>=<VALUE>]...]\n"
" [kpaths <start_gfid> <end_gfid> <k> "
"[-alo NAME=VALUE]...]\n"
" [resource [-alo <NAME>=<VALUE>]...]\n"
" [-ds <ds_name>][-f <ds_format>][-l <layer_name>]\n"
" [-dsco <NAME>=<VALUE>]... [-lco <NAME>=<VALUE>]...\n"
" <gnm_name>\n");
if (bShort)
{
printf("\nNote: gnmanalyse --long-usage for full help.\n");
if (pszAdditionalMsg)
fprintf(stderr, "\nFAILURE: %s\n", pszAdditionalMsg);
exit(1);
}
fprintf(bIsError ? stderr : stdout,
"\n dijkstra start_gfid end_gfid: calculates the best path "
"between two points using Dijkstra algorithm from start_gfid point "
"to end_gfid point\n"
" kpaths start_gfid end_gfid k: calculates k (up to 10) best "
"paths between two points using Yen\'s algorithm (which internally "
"uses Dijkstra algorithm for single path calculating) from "
"start_gfid point to end_gfid point\n"
" resource: calculates the \"resource distribution\". The "
"connected components search is performed using breadth-first "
"search and starting from that features which are marked by rules "
"as \'EMITTERS\'\n"
" -ds ds_name: the name&path of the dataset to save the layer "
"with resulting paths. Not need to be existed dataset\n"
" -f ds_format: define this to set the format of newly created "
"dataset\n"
" -l layer_name: the name of the resulting layer. If the layer "
"exists already - it will be rewritten. For K shortest paths "
"several layers are created in format layer_nameN, where N - is "
"number of the path (0 - is the most shortest one)\n"
" -dsco NAME=VALUE: Dataset creation option (format specific)\n"
" -lco NAME=VALUE: Layer creation option (format specific)\n"
" -alo NAME=VALUE: Algorithm option (format specific)\n"
" gnm_name: the network to work with (path and name)\n");
if (pszAdditionalMsg)
fprintf(stderr, "\nFAILURE: %s\n", pszAdditionalMsg);
exit(bIsError ? 1 : 0);
}
/************************************************************************/
/* GetLayerAndOverwriteIfNecessary() */
/************************************************************************/
static OGRLayer *GetLayerAndOverwriteIfNecessary(GDALDataset *poDstDS,
const char *pszNewLayerName,
int bOverwrite,
int *pbErrorOccurred)
{
if (pbErrorOccurred)
*pbErrorOccurred = FALSE;
/* GetLayerByName() can instantiate layers that would have been */
/* 'hidden' otherwise, for example, non-spatial tables in a */
/* PostGIS-enabled database, so this apparently useless command is */
/* not useless... (#4012) */
CPLPushErrorHandler(CPLQuietErrorHandler);
OGRLayer *poDstLayer = poDstDS->GetLayerByName(pszNewLayerName);
CPLPopErrorHandler();
CPLErrorReset();
int iLayer = -1;
if (poDstLayer != nullptr)
{
int nLayerCount = poDstDS->GetLayerCount();
for (iLayer = 0; iLayer < nLayerCount; iLayer++)
{
OGRLayer *poLayer = poDstDS->GetLayer(iLayer);
if (poLayer == poDstLayer)
break;
}
if (iLayer == nLayerCount)
/* should not happen with an ideal driver */
poDstLayer = nullptr;
}
/* -------------------------------------------------------------------- */
/* 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 != nullptr && bOverwrite)
{
if (poDstDS->DeleteLayer(iLayer) != OGRERR_NONE)
{
fprintf(stderr, "DeleteLayer() failed when overwrite requested.\n");
if (pbErrorOccurred)
*pbErrorOccurred = TRUE;
}
poDstLayer = nullptr;
}
return poDstLayer;
}
/************************************************************************/
/* CreateAndFillOutputDataset */
/************************************************************************/
static OGRErr CreateAndFillOutputDataset(OGRLayer *poSrcLayer,
const char *pszDestDataSource,
const char *pszFormat,
const char *pszLayer, char **papszDSCO,
char **papszLCO, int bQuiet)
{
GDALDriver *poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
if (poDriver == nullptr)
{
fprintf(stderr, "%s driver not available\n", pszFormat);
return OGRERR_FAILURE;
}
if (!CPLTestBool(CSLFetchNameValueDef(poDriver->GetMetadata(),
GDAL_DCAP_CREATE, "FALSE")))
{
fprintf(stderr, "%s driver does not support data source creation.\n",
pszFormat);
return OGRERR_FAILURE;
}
GDALDataset *poODS =
poDriver->Create(pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO);
if (poODS == nullptr)
{
fprintf(stderr, "%s driver failed to create %s\n", pszFormat,
pszDestDataSource);
return OGRERR_FAILURE;
}
if (nullptr == pszLayer)
pszLayer = poSrcLayer->GetName();
int nError;
GetLayerAndOverwriteIfNecessary(poODS, pszLayer, TRUE, &nError);
if (nError == TRUE)
{
return OGRERR_FAILURE;
}
// create layer
OGRLayer *poLayer = poODS->CopyLayer(poSrcLayer, pszLayer, papszLCO);
if (nullptr == poLayer)
{
fprintf(stderr, "\nFAILURE: Can not copy path to %s\n",
pszDestDataSource);
GDALClose(poODS);
return OGRERR_FAILURE;
}
if (bQuiet == FALSE)
{
printf("\nPath successfully copied and added to the network at %s\n",
pszDestDataSource);
}
GDALClose(poODS);
return OGRERR_NONE;
}
/************************************************************************/
/* ReportOnLayer() */
/************************************************************************/
static void ReportOnLayer(OGRLayer *poLayer, int bVerbose)
{
OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
/* -------------------------------------------------------------------- */
/* Report various overall information. */
/* -------------------------------------------------------------------- */
printf("\n");
printf("Layer name: %s\n", poLayer->GetName());
if (bVerbose)
{
int nGeomFieldCount = poLayer->GetLayerDefn()->GetGeomFieldCount();
if (nGeomFieldCount > 1)
{
for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
{
OGRGeomFieldDefn *poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
printf("Geometry (%s): %s\n", poGFldDefn->GetNameRef(),
OGRGeometryTypeToName(poGFldDefn->GetType()));
}
}
else
{
printf("Geometry: %s\n",
OGRGeometryTypeToName(poLayer->GetGeomType()));
}
printf("Feature Count: " CPL_FRMT_GIB "\n", poLayer->GetFeatureCount());
OGREnvelope oExt;
if (nGeomFieldCount > 1)
{
for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
{
if (poLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE)
{
OGRGeomFieldDefn *poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
CPLprintf("Extent (%s): (%f, %f) - (%f, %f)\n",
poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY,
oExt.MaxX, oExt.MaxY);
}
}
}
else if (poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE)
{
CPLprintf("Extent: (%f, %f) - (%f, %f)\n", oExt.MinX, oExt.MinY,
oExt.MaxX, oExt.MaxY);
}
char *pszWKT;
if (nGeomFieldCount > 1)
{
for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
{
OGRGeomFieldDefn *poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
const OGRSpatialReference *poSRS = poGFldDefn->GetSpatialRef();
if (poSRS == nullptr)
pszWKT = CPLStrdup("(unknown)");
else
{
poSRS->exportToPrettyWkt(&pszWKT);
}
printf("SRS WKT (%s):\n%s\n", poGFldDefn->GetNameRef(), pszWKT);
CPLFree(pszWKT);
}
}
else
{
if (poLayer->GetSpatialRef() == nullptr)
pszWKT = CPLStrdup("(unknown)");
else
{
poLayer->GetSpatialRef()->exportToPrettyWkt(&pszWKT);
}
printf("Layer SRS WKT:\n%s\n", pszWKT);
CPLFree(pszWKT);
}
if (strlen(poLayer->GetFIDColumn()) > 0)
printf("FID Column = %s\n", poLayer->GetFIDColumn());
for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
{
OGRGeomFieldDefn *poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
if (nGeomFieldCount == 1 && EQUAL(poGFldDefn->GetNameRef(), "") &&
poGFldDefn->IsNullable())
break;
printf("Geometry Column ");
if (nGeomFieldCount > 1)
printf("%d ", iGeom + 1);
if (!poGFldDefn->IsNullable())
printf("NOT NULL ");
printf("= %s\n", poGFldDefn->GetNameRef());
}
for (int iAttr = 0; iAttr < poDefn->GetFieldCount(); iAttr++)
{
OGRFieldDefn *poField = poDefn->GetFieldDefn(iAttr);
const char *pszType =
(poField->GetSubType() != OFSTNone)
? CPLSPrintf(
"%s(%s)",
poField->GetFieldTypeName(poField->GetType()),
poField->GetFieldSubTypeName(poField->GetSubType()))
: poField->GetFieldTypeName(poField->GetType());
printf("%s: %s (%d.%d)", poField->GetNameRef(), pszType,
poField->GetWidth(), poField->GetPrecision());
if (!poField->IsNullable())
printf(" NOT NULL");
if (poField->GetDefault() != nullptr)
printf(" DEFAULT %s", poField->GetDefault());
printf("\n");
}
}
/* -------------------------------------------------------------------- */
/* Read, and dump features. */
/* -------------------------------------------------------------------- */
for (auto &poFeature : poLayer)
{
poFeature->DumpReadable(nullptr);
}
}
/************************************************************************/
/* main() */
/************************************************************************/
#define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg) \
do \
{ \
if (iArg + nExtraArg >= nArgc) \
Usage(true, CPLSPrintf("%s option requires %d argument(s)", \
papszArgv[iArg], nExtraArg)); \
} while (false)
MAIN_START(nArgc, papszArgv)
{
int bQuiet = FALSE;
const char *pszDataSource = nullptr;
GNMGFID nFromFID = -1;
GNMGFID nToFID = -1;
int nK = 1;
const char *pszDataset = nullptr;
const char *pszFormat = "ESRI Shapefile";
const char *pszLayer = nullptr;
GNMNetwork *poDS = nullptr;
OGRLayer *poResultLayer = nullptr;
char **papszDSCO = nullptr, **papszLCO = nullptr, **papszALO = nullptr;
operation stOper = op_unknown;
int nRet = 0;
// Check strict compilation and runtime library version as we use C++ API
if (!GDAL_CHECK_VERSION(papszArgv[0]))
exit(1);
EarlySetConfigOptions(nArgc, papszArgv);
/* -------------------------------------------------------------------- */
/* Register format(s). */
/* -------------------------------------------------------------------- */
GDALAllRegister();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
nArgc = GDALGeneralCmdLineProcessor(nArgc, &papszArgv, GDAL_OF_GNM);
if (nArgc < 1)
{
exit(-nArgc);
}
for (int iArg = 1; iArg < nArgc; iArg++)
{
if (EQUAL(papszArgv[1], "--utility_version"))
{
printf("%s was compiled against GDAL %s and is running against "
"GDAL %s\n",
papszArgv[0], GDAL_RELEASE_NAME,
GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(papszArgv);
return 0;
}
else if (EQUAL(papszArgv[iArg], "--help"))
{
Usage(false);
}
else if (EQUAL(papszArgv[iArg], "--long-usage"))
{
Usage(false, nullptr, false);
}
else if (EQUAL(papszArgv[iArg], "-q") ||
EQUAL(papszArgv[iArg], "-quiet"))
{
bQuiet = TRUE;
}
else if (EQUAL(papszArgv[iArg], "dijkstra"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(2);
stOper = op_dijkstra;
nFromFID = atoi(papszArgv[++iArg]);
nToFID = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "kpaths"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(3);
stOper = op_kpaths;
nFromFID = atoi(papszArgv[++iArg]);
nToFID = atoi(papszArgv[++iArg]);
nK = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "resource"))
{
stOper = op_resource;
}
else if (EQUAL(papszArgv[iArg], "-ds"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszDataset = papszArgv[++iArg];
}
else if ((EQUAL(papszArgv[iArg], "-f") ||
EQUAL(papszArgv[iArg], "-of")))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszFormat = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-l"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszLayer = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-dsco"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
papszDSCO = CSLAddString(papszDSCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-lco"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
papszLCO = CSLAddString(papszLCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-alo"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
papszALO = CSLAddString(papszALO, papszArgv[++iArg]);
}
else if (papszArgv[iArg][0] == '-')
{
Usage(true,
CPLSPrintf("Unknown option name '%s'", papszArgv[iArg]));
}
else if (pszDataSource == nullptr)
pszDataSource = papszArgv[iArg];
}
// do the work
// ////////////////////////////////////////////////////////////////
if (stOper == op_dijkstra)
{
if (pszDataSource == nullptr)
Usage(true, "No network dataset provided");
if (nFromFID == -1 || nToFID == -1)
Usage(true, "Invalid input from or to identificators");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
poResultLayer =
poDS->GetPath(nFromFID, nToFID, GATDijkstraShortestPath, papszALO);
if (nullptr == pszDataset)
{
ReportOnLayer(poResultLayer, bQuiet == FALSE);
}
else
{
if (CreateAndFillOutputDataset(poResultLayer, pszDataset, pszFormat,
pszLayer, papszDSCO, papszLCO,
bQuiet) != OGRERR_NONE)
{
nRet = 1;
goto exit;
}
}
}
else if (stOper == op_kpaths)
{
if (pszDataSource == nullptr)
Usage(true, "No network dataset provided");
if (nFromFID == -1 || nToFID == -1)
Usage(true, "Invalid input from or to identificators");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
if (CSLFindName(papszALO, GNM_MD_NUM_PATHS) == -1)
{
CPLDebug("GNM", "No K in options, add %d value", nK);
papszALO = CSLAddNameValue(papszALO, GNM_MD_NUM_PATHS,
CPLSPrintf("%d", nK));
}
poResultLayer =
poDS->GetPath(nFromFID, nToFID, GATKShortestPath, papszALO);
if (nullptr == pszDataset)
{
ReportOnLayer(poResultLayer, bQuiet == FALSE);
}
else
{
if (CreateAndFillOutputDataset(poResultLayer, pszDataset, pszFormat,
pszLayer, papszDSCO, papszLCO,
bQuiet) != OGRERR_NONE)
{
nRet = 1;
goto exit;
}
}
}
else if (stOper == op_resource)
{
if (pszDataSource == nullptr)
Usage(true, "No network dataset provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
poResultLayer =
poDS->GetPath(nFromFID, nToFID, GATConnectedComponents, papszALO);
if (nullptr == pszDataset)
{
ReportOnLayer(poResultLayer, bQuiet == FALSE);
}
else
{
if (CreateAndFillOutputDataset(poResultLayer, pszDataset, pszFormat,
pszLayer, papszDSCO, papszLCO,
bQuiet) != OGRERR_NONE)
{
nRet = 1;
goto exit;
}
}
}
else
{
fprintf(
stderr,
"Need an operation. See help what you can do with gnmanalyse:\n");
Usage(true);
}
exit:
CSLDestroy(papszDSCO);
CSLDestroy(papszLCO);
CSLDestroy(papszALO);
CSLDestroy(papszArgv);
if (poResultLayer != nullptr)
poDS->ReleaseResultSet(poResultLayer);
if (poDS)
{
if (GDALClose(poDS) != CE_None)
nRet = 1;
}
GDALDestroyDriverManager();
return nRet;
}
MAIN_END