forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapogr.cpp
3155 lines (2690 loc) · 110 KB
/
mapogr.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
/**********************************************************************
* $Id$
*
* Project: MapServer
* Purpose: OGR Link
* Author: Daniel Morissette, DM Solutions Group (morissette@dmsolutions.ca)
* Frank Warmerdam (warmerdam@pobox.com)
*
**********************************************************************
* Copyright (c) 2000-2005, Daniel Morissette, DM Solutions Group Inc
*
* 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 of this Software or works derived from this 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 <assert.h>
#include "mapserver.h"
#include "mapproject.h"
#include "mapthread.h"
#if defined(USE_OGR) || defined(USE_GDAL)
# include "gdal_version.h"
# include "cpl_conv.h"
# include "cpl_string.h"
# include "ogr_srs_api.h"
#endif
#define ACQUIRE_OGR_LOCK msAcquireLock( TLOCK_OGR )
#define RELEASE_OGR_LOCK msReleaseLock( TLOCK_OGR )
#ifdef USE_OGR
#include "ogr_api.h"
typedef struct ms_ogr_file_info_t {
char *pszFname;
int nLayerIndex;
OGRDataSourceH hDS;
OGRLayerH hLayer;
OGRFeatureH hLastFeature;
int nTileId; /* applies on the tiles themselves. */
struct ms_ogr_file_info_t *poCurTile; /* exists on tile index, -> tiles */
rectObj rect; /* set by WhichShapes */
int last_record_index_read;
} msOGRFileInfo;
static int msOGRLayerIsOpen(layerObj *layer);
static int msOGRLayerInitItemInfo(layerObj *layer);
static int msOGRLayerGetAutoStyle(mapObj *map, layerObj *layer, classObj *c,
shapeObj* shape);
static void msOGRCloseConnection( void *conn_handle );
/* ==================================================================
* Geometry conversion functions
* ================================================================== */
/**********************************************************************
* ogrPointsAddPoint()
*
* NOTE: This function assumes the line->point array already has been
* allocated large enough for the point to be added, but that numpoints
* does not include this new point.
**********************************************************************/
static void ogrPointsAddPoint(lineObj *line, double dX, double dY,
int lineindex, rectObj *bounds)
{
/* Keep track of shape bounds */
if (line->numpoints == 0 && lineindex == 0) {
bounds->minx = bounds->maxx = dX;
bounds->miny = bounds->maxy = dY;
} else {
if (dX < bounds->minx) bounds->minx = dX;
if (dX > bounds->maxx) bounds->maxx = dX;
if (dY < bounds->miny) bounds->miny = dY;
if (dY > bounds->maxy) bounds->maxy = dY;
}
line->point[line->numpoints].x = dX;
line->point[line->numpoints].y = dY;
#ifdef USE_POINT_Z_M
line->point[line->numpoints].z = 0.0;
line->point[line->numpoints].m = 0.0;
#endif
line->numpoints++;
}
/**********************************************************************
* ogrGeomPoints()
**********************************************************************/
static int ogrGeomPoints(OGRGeometryH hGeom, shapeObj *outshp)
{
int i;
int numpoints;
if (hGeom == NULL)
return 0;
OGRwkbGeometryType eGType = wkbFlatten( OGR_G_GetGeometryType( hGeom ) );
/* -------------------------------------------------------------------- */
/* Container types result in recursive invocation on each */
/* subobject to add a set of points to the current list. */
/* -------------------------------------------------------------------- */
switch( eGType ) {
case wkbGeometryCollection:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbPolygon: {
/* Treat it as GeometryCollection */
for (int iGeom=0; iGeom < OGR_G_GetGeometryCount( hGeom ); iGeom++ ) {
if( ogrGeomPoints( OGR_G_GetGeometryRef( hGeom, iGeom ),
outshp ) == -1 )
return -1;
}
return 0;
}
break;
case wkbPoint:
case wkbMultiPoint:
case wkbLineString:
case wkbLinearRing:
/* We will handle these directly */
break;
default:
/* There shouldn't be any more cases should there? */
msSetError(MS_OGRERR,
"OGRGeometry type `%s' not supported yet.",
"ogrGeomPoints()",
OGR_G_GetGeometryName( hGeom ) );
return(-1);
}
/* ------------------------------------------------------------------
* Count total number of points
* ------------------------------------------------------------------ */
if ( eGType == wkbPoint ) {
numpoints = 1;
} else if ( eGType == wkbLineString
|| eGType == wkbLinearRing ) {
numpoints = OGR_G_GetPointCount( hGeom );
} else if ( eGType == wkbMultiPoint ) {
numpoints = OGR_G_GetGeometryCount( hGeom );
} else {
msSetError(MS_OGRERR,
"OGRGeometry type `%s' not supported yet.",
"ogrGeomPoints()",
OGR_G_GetGeometryName( hGeom ) );
return(-1);
}
/* ------------------------------------------------------------------
* Do we need to allocate a line object to contain all our points?
* ------------------------------------------------------------------ */
if( outshp->numlines == 0 ) {
lineObj newline;
newline.numpoints = 0;
newline.point = NULL;
msAddLine(outshp, &newline);
}
/* ------------------------------------------------------------------
* Extend the point array for the new of points to add from the
* current geometry.
* ------------------------------------------------------------------ */
lineObj *line = outshp->line + outshp->numlines-1;
if( line->point == NULL )
line->point = (pointObj *) malloc(sizeof(pointObj) * numpoints);
else
line->point = (pointObj *)
realloc(line->point,sizeof(pointObj) * (numpoints+line->numpoints));
if(!line->point) {
msSetError(MS_MEMERR, "Unable to allocate temporary point cache.",
"ogrGeomPoints()");
return(-1);
}
/* ------------------------------------------------------------------
* alloc buffer and filter/transform points
* ------------------------------------------------------------------ */
if( eGType == wkbPoint ) {
ogrPointsAddPoint(line, OGR_G_GetX(hGeom, 0), OGR_G_GetY(hGeom, 0),
outshp->numlines-1, &(outshp->bounds));
} else if( eGType == wkbLineString
|| eGType == wkbLinearRing ) {
for(i=0; i<numpoints; i++)
ogrPointsAddPoint(line, OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
outshp->numlines-1, &(outshp->bounds));
} else if( eGType == wkbMultiPoint ) {
for(i=0; i<numpoints; i++) {
OGRGeometryH hPoint = OGR_G_GetGeometryRef( hGeom, i );
ogrPointsAddPoint(line, OGR_G_GetX(hPoint, 0), OGR_G_GetY(hPoint, 0),
outshp->numlines-1, &(outshp->bounds));
}
}
outshp->type = MS_SHAPE_POINT;
return(0);
}
/**********************************************************************
* ogrGeomLine()
*
* Recursively convert any OGRGeometry into a shapeObj. Each part becomes
* a line in the overall shapeObj.
**********************************************************************/
static int ogrGeomLine(OGRGeometryH hGeom, shapeObj *outshp,
int bCloseRings)
{
if (hGeom == NULL)
return 0;
/* ------------------------------------------------------------------
* Use recursive calls for complex geometries
* ------------------------------------------------------------------ */
OGRwkbGeometryType eGType = wkbFlatten( OGR_G_GetGeometryType( hGeom ) );
if ( eGType == wkbPolygon
|| eGType == wkbGeometryCollection
|| eGType == wkbMultiLineString
|| eGType == wkbMultiPolygon ) {
if (eGType == wkbPolygon && outshp->type == MS_SHAPE_NULL)
outshp->type = MS_SHAPE_POLYGON;
/* Treat it as GeometryCollection */
for (int iGeom=0; iGeom < OGR_G_GetGeometryCount( hGeom ); iGeom++ ) {
if( ogrGeomLine( OGR_G_GetGeometryRef( hGeom, iGeom ),
outshp, bCloseRings ) == -1 )
return -1;
}
}
/* ------------------------------------------------------------------
* OGRPoint and OGRMultiPoint
* ------------------------------------------------------------------ */
else if ( eGType == wkbPoint || eGType == wkbMultiPoint ) {
/* Hummmm a point when we're drawing lines/polygons... just drop it! */
}
/* ------------------------------------------------------------------
* OGRLinearRing/OGRLineString ... both are of type wkbLineString
* ------------------------------------------------------------------ */
else if ( eGType == wkbLineString ) {
int j, numpoints;
lineObj line= {0,NULL};
double dX, dY;
if ((numpoints = OGR_G_GetPointCount( hGeom )) < 2)
return 0;
if (outshp->type == MS_SHAPE_NULL)
outshp->type = MS_SHAPE_LINE;
line.numpoints = 0;
line.point = (pointObj *)malloc(sizeof(pointObj)*(numpoints+1));
if(!line.point) {
msSetError(MS_MEMERR, "Unable to allocate temporary point cache.",
"ogrGeomLine");
return(-1);
}
#if GDAL_VERSION_NUM >= 1900
OGR_G_GetPoints(hGeom,
&(line.point[0].x), sizeof(pointObj),
&(line.point[0].y), sizeof(pointObj),
NULL, 0);
#endif
for(j=0; j<numpoints; j++) {
#if GDAL_VERSION_NUM < 1900
dX = line.point[j].x = OGR_G_GetX( hGeom, j);
dY = line.point[j].y = OGR_G_GetY( hGeom, j);
#else
dX = line.point[j].x;
dY = line.point[j].y;
#endif
/* Keep track of shape bounds */
if (j == 0 && outshp->numlines == 0) {
outshp->bounds.minx = outshp->bounds.maxx = dX;
outshp->bounds.miny = outshp->bounds.maxy = dY;
} else {
if (dX < outshp->bounds.minx) outshp->bounds.minx = dX;
if (dX > outshp->bounds.maxx) outshp->bounds.maxx = dX;
if (dY < outshp->bounds.miny) outshp->bounds.miny = dY;
if (dY > outshp->bounds.maxy) outshp->bounds.maxy = dY;
}
}
line.numpoints = numpoints;
if (bCloseRings &&
( line.point[line.numpoints-1].x != line.point[0].x ||
line.point[line.numpoints-1].y != line.point[0].y ) ) {
line.point[line.numpoints].x = line.point[0].x;
line.point[line.numpoints].y = line.point[0].y;
line.numpoints++;
}
msAddLineDirectly(outshp, &line);
} else {
msSetError(MS_OGRERR,
"OGRGeometry type `%s' not supported.",
"ogrGeomLine()",
OGR_G_GetGeometryName( hGeom ) );
return(-1);
}
return(0);
}
/**********************************************************************
* ogrConvertGeometry()
*
* Convert OGR geometry into a shape object doing the best possible
* job to match OGR Geometry type and layer type.
*
* If layer type is incompatible with geometry, then shape is returned with
* shape->type = MS_SHAPE_NULL
**********************************************************************/
static int ogrConvertGeometry(OGRGeometryH hGeom, shapeObj *outshp,
enum MS_LAYER_TYPE layertype)
{
/* ------------------------------------------------------------------
* Process geometry according to layer type
* ------------------------------------------------------------------ */
int nStatus = MS_SUCCESS;
if (hGeom == NULL) {
// Empty geometry... this is not an error... we'll just skip it
return MS_SUCCESS;
}
switch(layertype) {
/* ------------------------------------------------------------------
* POINT layer - Any geometry can be converted to point/multipoint
* ------------------------------------------------------------------ */
case MS_LAYER_POINT:
if(ogrGeomPoints(hGeom, outshp) == -1) {
nStatus = MS_FAILURE; // Error message already produced.
}
break;
/* ------------------------------------------------------------------
* LINE layer
* ------------------------------------------------------------------ */
case MS_LAYER_LINE:
if(ogrGeomLine(hGeom, outshp, MS_FALSE) == -1) {
nStatus = MS_FAILURE; // Error message already produced.
}
if (outshp->type != MS_SHAPE_LINE && outshp->type != MS_SHAPE_POLYGON)
outshp->type = MS_SHAPE_NULL; // Incompatible type for this layer
break;
/* ------------------------------------------------------------------
* POLYGON layer
* ------------------------------------------------------------------ */
case MS_LAYER_POLYGON:
if(ogrGeomLine(hGeom, outshp, MS_TRUE) == -1) {
nStatus = MS_FAILURE; // Error message already produced.
}
if (outshp->type != MS_SHAPE_POLYGON)
outshp->type = MS_SHAPE_NULL; // Incompatible type for this layer
break;
/* ------------------------------------------------------------------
* MS_ANNOTATION layer - return real feature type
* ------------------------------------------------------------------ */
case MS_LAYER_ANNOTATION:
case MS_LAYER_CHART:
case MS_LAYER_QUERY:
switch( OGR_G_GetGeometryType( hGeom ) ) {
case wkbPoint:
case wkbPoint25D:
case wkbMultiPoint:
case wkbMultiPoint25D:
if(ogrGeomPoints(hGeom, outshp) == -1) {
nStatus = MS_FAILURE; // Error message already produced.
}
break;
default:
// Handle any non-point types as lines/polygons ... ogrGeomLine()
// will decide the shape type
if(ogrGeomLine(hGeom, outshp, MS_FALSE) == -1) {
nStatus = MS_FAILURE; // Error message already produced.
}
}
break;
default:
msSetError(MS_MISCERR, "Unknown or unsupported layer type.",
"msOGRLayerNextShape()");
nStatus = MS_FAILURE;
} /* switch layertype */
return nStatus;
}
/**********************************************************************
* msOGRGeometryToShape()
*
* Utility function to convert from OGR geometry to a mapserver shape
* object.
**********************************************************************/
int msOGRGeometryToShape(OGRGeometryH hGeometry, shapeObj *psShape,
OGRwkbGeometryType nType)
{
if (hGeometry && psShape && nType > 0) {
if (nType == wkbPoint || nType == wkbMultiPoint )
return ogrConvertGeometry(hGeometry, psShape, MS_LAYER_POINT);
else if (nType == wkbLineString || nType == wkbMultiLineString)
return ogrConvertGeometry(hGeometry, psShape, MS_LAYER_LINE);
else if (nType == wkbPolygon || nType == wkbMultiPolygon)
return ogrConvertGeometry(hGeometry, psShape, MS_LAYER_POLYGON);
else
return MS_FAILURE;
} else
return MS_FAILURE;
}
/* ==================================================================
* Attributes handling functions
* ================================================================== */
// Special field index codes for handling text string and angle coming from
// OGR style strings.
#define MSOGR_LABELNUMITEMS 21
#define MSOGR_LABELFONTNAMENAME "OGR:LabelFont"
#define MSOGR_LABELFONTNAMEINDEX -100
#define MSOGR_LABELSIZENAME "OGR:LabelSize"
#define MSOGR_LABELSIZEINDEX -101
#define MSOGR_LABELTEXTNAME "OGR:LabelText"
#define MSOGR_LABELTEXTINDEX -102
#define MSOGR_LABELANGLENAME "OGR:LabelAngle"
#define MSOGR_LABELANGLEINDEX -103
#define MSOGR_LABELFCOLORNAME "OGR:LabelFColor"
#define MSOGR_LABELFCOLORINDEX -104
#define MSOGR_LABELBCOLORNAME "OGR:LabelBColor"
#define MSOGR_LABELBCOLORINDEX -105
#define MSOGR_LABELPLACEMENTNAME "OGR:LabelPlacement"
#define MSOGR_LABELPLACEMENTINDEX -106
#define MSOGR_LABELANCHORNAME "OGR:LabelAnchor"
#define MSOGR_LABELANCHORINDEX -107
#define MSOGR_LABELDXNAME "OGR:LabelDx"
#define MSOGR_LABELDXINDEX -108
#define MSOGR_LABELDYNAME "OGR:LabelDy"
#define MSOGR_LABELDYINDEX -109
#define MSOGR_LABELPERPNAME "OGR:LabelPerp"
#define MSOGR_LABELPERPINDEX -110
#define MSOGR_LABELBOLDNAME "OGR:LabelBold"
#define MSOGR_LABELBOLDINDEX -111
#define MSOGR_LABELITALICNAME "OGR:LabelItalic"
#define MSOGR_LABELITALICINDEX -112
#define MSOGR_LABELUNDERLINENAME "OGR:LabelUnderline"
#define MSOGR_LABELUNDERLINEINDEX -113
#define MSOGR_LABELPRIORITYNAME "OGR:LabelPriority"
#define MSOGR_LABELPRIORITYINDEX -114
#define MSOGR_LABELSTRIKEOUTNAME "OGR:LabelStrikeout"
#define MSOGR_LABELSTRIKEOUTINDEX -115
#define MSOGR_LABELSTRETCHNAME "OGR:LabelStretch"
#define MSOGR_LABELSTRETCHINDEX -116
#define MSOGR_LABELADJHORNAME "OGR:LabelAdjHor"
#define MSOGR_LABELADJHORINDEX -117
#define MSOGR_LABELADJVERTNAME "OGR:LabelAdjVert"
#define MSOGR_LABELADJVERTINDEX -118
#define MSOGR_LABELHCOLORNAME "OGR:LabelHColor"
#define MSOGR_LABELHCOLORINDEX -119
#define MSOGR_LABELOCOLORNAME "OGR:LabelOColor"
#define MSOGR_LABELOCOLORINDEX -120
/**********************************************************************
* msOGRGetValues()
*
* Load selected item (i.e. field) values into a char array
*
* Some special attribute names are used to return some OGRFeature params
* like for instance stuff encoded in the OGRStyleString.
* For now the following pseudo-attribute names are supported:
* "OGR:TextString" OGRFeatureStyle's text string if present
* "OGR:TextAngle" OGRFeatureStyle's text angle, or 0 if not set
**********************************************************************/
static char **msOGRGetValues(layerObj *layer, OGRFeatureH hFeature)
{
char **values;
const char *pszValue = NULL;
int i;
if(layer->numitems == 0)
return(NULL);
if(!layer->iteminfo) // Should not happen... but just in case!
if (msOGRLayerInitItemInfo(layer) != MS_SUCCESS)
return NULL;
if((values = (char **)malloc(sizeof(char *)*layer->numitems)) == NULL) {
msSetError(MS_MEMERR, NULL, "msOGRGetValues()");
return(NULL);
}
OGRStyleMgrH hStyleMgr = NULL;
OGRStyleToolH hLabelStyle = NULL;
int *itemindexes = (int*)layer->iteminfo;
for(i=0; i<layer->numitems; i++) {
if (itemindexes[i] >= 0) {
// Extract regular attributes
values[i] = msStrdup(OGR_F_GetFieldAsString( hFeature, itemindexes[i]));
} else {
// Handle special OGR attributes coming from StyleString
if (!hStyleMgr) {
hStyleMgr = OGR_SM_Create(NULL);
OGR_SM_InitFromFeature(hStyleMgr, hFeature);
OGRStyleToolH hStylePart = OGR_SM_GetPart(hStyleMgr, 0, NULL);
if (hStylePart && OGR_ST_GetType(hStylePart) == OGRSTCLabel)
hLabelStyle = hStylePart;
else if (hStylePart) {
OGR_ST_Destroy(hStylePart);
hStylePart = NULL;
}
/* Setting up the size units according to msOGRLayerGetAutoStyle*/
if (hStylePart && layer->map)
OGR_ST_SetUnit(hStylePart, OGRSTUPixel, layer->map->cellsize*72.0*39.37);
}
int bDefault;
if (itemindexes[i] == MSOGR_LABELTEXTINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelTextString,
&bDefault)) == NULL))
values[i] = msStrdup("");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELTEXTNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELANGLEINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelAngle,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELANGLENAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELSIZEINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelSize,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELSIZENAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELFCOLORINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelFColor,
&bDefault)) == NULL))
values[i] = msStrdup("#000000");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELFCOLORNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELFONTNAMEINDEX ) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelFontName,
&bDefault)) == NULL))
values[i] = msStrdup("Arial");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELFONTNAMENAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELBCOLORINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelBColor,
&bDefault)) == NULL))
values[i] = msStrdup("#000000");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELBCOLORNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELPLACEMENTINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelPlacement,
&bDefault)) == NULL))
values[i] = msStrdup("");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELPLACEMENTNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELANCHORINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelAnchor,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELANCHORNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELDXINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelDx,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELDXNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELDYINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelDy,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELDYNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELPERPINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelPerp,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELPERPNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELBOLDINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelBold,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELBOLDNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELITALICINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelItalic,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELITALICNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELUNDERLINEINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelUnderline,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELUNDERLINENAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELPRIORITYINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelPriority,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELPRIORITYNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELSTRIKEOUTINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelStrikeout,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELSTRIKEOUTNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELSTRETCHINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelStretch,
&bDefault)) == NULL))
values[i] = msStrdup("0");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELSTRETCHNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELADJHORINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelAdjHor,
&bDefault)) == NULL))
values[i] = msStrdup("");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELADJHORNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELADJVERTINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelAdjVert,
&bDefault)) == NULL))
values[i] = msStrdup("");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELADJVERTNAME " = \"%s\"\n", values[i]);
} else if (itemindexes[i] == MSOGR_LABELHCOLORINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelHColor,
&bDefault)) == NULL))
values[i] = msStrdup("");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELHCOLORNAME " = \"%s\"\n", values[i]);
}
#if GDAL_VERSION_NUM >= 1600
else if (itemindexes[i] == MSOGR_LABELOCOLORINDEX) {
if (hLabelStyle == NULL
|| ((pszValue = OGR_ST_GetParamStr(hLabelStyle,
OGRSTLabelOColor,
&bDefault)) == NULL))
values[i] = msStrdup("");
else
values[i] = msStrdup(pszValue);
if (layer->debug >= MS_DEBUGLEVEL_VVV)
msDebug(MSOGR_LABELOCOLORNAME " = \"%s\"\n", values[i]);
}
#endif /* GDAL_VERSION_NUM >= 1600 */
else {
msSetError(MS_OGRERR,"Invalid field index!?!","msOGRGetValues()");
return(NULL);
}
}
}
OGR_SM_Destroy(hStyleMgr);
OGR_ST_Destroy(hLabelStyle);
return(values);
}
#endif /* USE_OGR */
#if defined(USE_OGR) || defined(USE_GDAL)
/**********************************************************************
* msOGRSpatialRef2ProjectionObj()
*
* Init a MapServer projectionObj using an OGRSpatialRef
* Works only with PROJECTION AUTO
*
* Returns MS_SUCCESS/MS_FAILURE
**********************************************************************/
static int msOGRSpatialRef2ProjectionObj(OGRSpatialReferenceH hSRS,
projectionObj *proj, int debug_flag )
{
#ifdef USE_PROJ
// First flush the "auto" name from the projargs[]...
msFreeProjection( proj );
if (hSRS == NULL || OSRIsLocal( hSRS ) ) {
// Dataset had no set projection or is NonEarth (LOCAL_CS)...
// Nothing else to do. Leave proj empty and no reprojection will happen!
return MS_SUCCESS;
}
// Export OGR SRS to a PROJ4 string
char *pszProj = NULL;
if (OSRExportToProj4( hSRS, &pszProj ) != OGRERR_NONE ||
pszProj == NULL || strlen(pszProj) == 0) {
msSetError(MS_OGRERR, "Conversion from OGR SRS to PROJ4 failed.",
"msOGRSpatialRef2ProjectionObj()");
CPLFree(pszProj);
return(MS_FAILURE);
}
if( debug_flag )
msDebug( "AUTO = %s\n", pszProj );
if( msLoadProjectionString( proj, pszProj ) != 0 )
return MS_FAILURE;
CPLFree(pszProj);
#endif
return MS_SUCCESS;
}
#endif // defined(USE_OGR) || defined(USE_GDAL)
/**********************************************************************
* msOGCWKT2ProjectionObj()
*
* Init a MapServer projectionObj using an OGC WKT definition.
* Works only with PROJECTION AUTO
*
* Returns MS_SUCCESS/MS_FAILURE
**********************************************************************/
int msOGCWKT2ProjectionObj( const char *pszWKT,
projectionObj *proj,
int debug_flag )
{
#if defined(USE_OGR) || defined(USE_GDAL)
OGRSpatialReferenceH hSRS;
char *pszAltWKT = (char *) pszWKT;
OGRErr eErr;
int ms_result;
hSRS = OSRNewSpatialReference( NULL );
if( !EQUALN(pszWKT,"GEOGCS",6)
&& !EQUALN(pszWKT,"PROJCS",6)
&& !EQUALN(pszWKT,"LOCAL_CS",8) )
eErr = OSRSetFromUserInput( hSRS, pszWKT );
else
eErr = OSRImportFromWkt( hSRS, &pszAltWKT );
if( eErr != OGRERR_NONE ) {
OSRDestroySpatialReference( hSRS );
msSetError(MS_OGRERR,
"Ingestion of WKT string '%s' failed.",
"msOGCWKT2ProjectionObj()",
pszWKT );
return MS_FAILURE;
}
ms_result = msOGRSpatialRef2ProjectionObj( hSRS, proj, debug_flag );
OSRDestroySpatialReference( hSRS );
return ms_result;
#else
msSetError(MS_OGRERR,
"Not implemented since neither OGR nor GDAL is enabled.",
"msOGCWKT2ProjectionObj()");
return MS_FAILURE;
#endif
}
/**********************************************************************
* msOGRFileOpen()
*
* Open an OGR connection, and initialize a msOGRFileInfo.
**********************************************************************/
#ifdef USE_OGR
static int bOGRDriversRegistered = MS_FALSE;
#endif
void msOGRInitialize(void)
{
#ifdef USE_OGR
/* ------------------------------------------------------------------
* Register OGR Drivers, only once per execution
* ------------------------------------------------------------------ */
if (!bOGRDriversRegistered) {
ACQUIRE_OGR_LOCK;
OGRRegisterAll();
CPLPushErrorHandler( CPLQuietErrorHandler );
/* ------------------------------------------------------------------
* Pass config option GML_FIELDTYPES=ALWAYS_STRING to OGR so that all
* GML attributes are returned as strings to MapServer. This is most efficient
* and prevents problems with autodetection of some attribute types.
* ------------------------------------------------------------------ */
CPLSetConfigOption("GML_FIELDTYPES","ALWAYS_STRING");
bOGRDriversRegistered = MS_TRUE;
RELEASE_OGR_LOCK;
}
#endif /* USE_OGR */
}
/* ==================================================================
* The following functions closely relate to the API called from
* maplayer.c, but are intended to be used for the tileindex or direct
* layer access.
* ================================================================== */
#ifdef USE_OGR
/**********************************************************************
* msOGRFileOpen()
*
* Open an OGR connection, and initialize a msOGRFileInfo.
**********************************************************************/
static msOGRFileInfo *
msOGRFileOpen(layerObj *layer, const char *connection )
{
char *conn_decrypted = NULL;
msOGRInitialize();
/* ------------------------------------------------------------------
* Make sure any encrypted token in the connection string are decrypted
* ------------------------------------------------------------------ */
if (connection) {
conn_decrypted = msDecryptStringTokens(layer->map, connection);
if (conn_decrypted == NULL)
return NULL; /* An error should already have been reported */
}
/* ------------------------------------------------------------------
* Parse connection string into dataset name, and layer name.
* ------------------------------------------------------------------ */
char *pszDSName = NULL, *pszLayerDef = NULL;
if( conn_decrypted == NULL ) {
/* we don't have anything */
} else if( layer->data != NULL ) {
pszDSName = CPLStrdup(conn_decrypted);
pszLayerDef = CPLStrdup(layer->data);
} else {
char **papszTokens = NULL;
papszTokens = CSLTokenizeStringComplex( conn_decrypted, ",", TRUE, FALSE );
if( CSLCount(papszTokens) > 0 )
pszDSName = CPLStrdup( papszTokens[0] );
if( CSLCount(papszTokens) > 1 )
pszLayerDef = CPLStrdup( papszTokens[1] );
CSLDestroy(papszTokens);
}
/* Get rid of decrypted connection string. We'll use the original (not
* decrypted) string for debug and error messages in the rest of the code.
*/
msFree(conn_decrypted);
conn_decrypted = NULL;
if( pszDSName == NULL ) {