forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWFSClient.cs
More file actions
1417 lines (1256 loc) · 65.5 KB
/
WFSClient.cs
File metadata and controls
1417 lines (1256 loc) · 65.5 KB
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
// WFS provider by Peter Robineau (peter.robineau@gmx.at)
// This file can be redistributed and/or modified under the terms of the GNU Lesser General Public License.
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Xml.XPath;
using GeoAPI.Geometries;
using SharpMap.CoordinateSystems;
using SharpMap.Utilities.Indexing;
using SharpMap.Utilities.SpatialIndexing;
using SharpMap.Utilities.Wfs;
namespace SharpMap.Data.Providers
{
/// <summary>
/// WFS dataprovider
/// This provider can be used to obtain data from an OGC Web Feature Service.
/// It performs the following requests: 'GetCapabilities', 'DescribeFeatureType' and 'GetFeature'.
/// This class is optimized for performing requests to GeoServer (http://geoserver.org).
/// Supported geometries are:
/// - PointPropertyType
/// - LineStringPropertyType
/// - PolygonPropertyType
/// - CurvePropertyType
/// - SurfacePropertyType
/// - MultiPointPropertyType
/// - MultiLineStringPropertyType
/// - MultiPolygonPropertyType
/// - MultiCurvePropertyType
/// - MultiSurfacePropertyType
/// </summary>
/// <example>
/// <code lang="C#">
///SharpMap.Map demoMap;
///
///const string getCapabilitiesURI = "http://localhost:8080/geoserver/wfs";
///const string serviceURI = "http://localhost:8080/geoserver/wfs";
///
///demoMap = new SharpMap.Map(new Size(600, 600));
///demoMap.MinimumZoom = 0.005;
///demoMap.BackColor = Color.White;
///
///SharpMap.Layers.VectorLayer layer1 = new SharpMap.Layers.VectorLayer("States");
///SharpMap.Layers.VectorLayer layer2 = new SharpMap.Layers.VectorLayer("SelectedStatesAndHousholds");
///SharpMap.Layers.VectorLayer layer3 = new SharpMap.Layers.VectorLayer("New Jersey");
///SharpMap.Layers.VectorLayer layer4 = new SharpMap.Layers.VectorLayer("Roads");
///SharpMap.Layers.VectorLayer layer5 = new SharpMap.Layers.VectorLayer("Landmarks");
///SharpMap.Layers.VectorLayer layer6 = new SharpMap.Layers.VectorLayer("Poi");
///
/// // Demo data from Geoserver 1.5.3 and Geoserver 1.6.0
///
///WFS prov1 = new WFS(getCapabilitiesURI, "topp", "states", WFS.WFSVersionEnum.WFS1_0_0);
///
/// // Bypass 'GetCapabilities' and 'DescribeFeatureType', if you know all necessary metadata.
///WfsFeatureTypeInfo featureTypeInfo = new WfsFeatureTypeInfo(serviceURI, "topp", null, "states", "the_geom");
/// // 'WFS.WFSVersionEnum.WFS1_1_0' supported by Geoserver 1.6.x
///WFS prov2 = new SharpMap.Data.Providers.WFS(featureTypeInfo, WFS.WFSVersionEnum.WFS1_1_0);
/// // Bypass 'GetCapabilities' and 'DescribeFeatureType' again...
/// // It's possible to specify the geometry type, if 'DescribeFeatureType' does not...(.e.g 'GeometryAssociationType')
/// // This helps to accelerate the initialization process in case of unprecise geometry information.
///WFS prov3 = new WFS(serviceURI, "topp", "http://www.openplans.org/topp", "states", "the_geom", GeometryTypeEnum.MultiSurfacePropertyType, WFS.WFSVersionEnum.WFS1_1_0);
///
/// // Get data-filled FeatureTypeInfo after initialization of dataprovider (useful in Web Applications for caching metadata.
///WfsFeatureTypeInfo info = prov1.FeatureTypeInfo;
///
/// // Use cached 'GetCapabilities' response of prov1 (featuretype hosted by same service).
/// // Compiled XPath expressions are re-used automatically!
/// // If you use a cached 'GetCapabilities' response make sure the data provider uses the same version of WFS as the one providing the cache!!!
///WFS prov4 = new WFS(prov1.GetCapabilitiesCache, "tiger", "tiger_roads", WFS.WFSVersionEnum.WFS1_0_0);
///WFS prov5 = new WFS(prov1.GetCapabilitiesCache, "tiger", "poly_landmarks", WFS.WFSVersionEnum.WFS1_0_0);
///WFS prov6 = new WFS(prov1.GetCapabilitiesCache, "tiger", "poi", WFS.WFSVersionEnum.WFS1_0_0);
/// // Clear cache of prov1 - data providers do not have any cache, if they use the one of another data provider
///prov1.GetCapabilitiesCache = null;
///
/// //Filters
///IFilter filter1 = new PropertyIsEqualToFilter_FE1_1_0("STATE_NAME", "California");
///IFilter filter2 = new PropertyIsEqualToFilter_FE1_1_0("STATE_NAME", "Vermont");
///IFilter filter3 = new PropertyIsBetweenFilter_FE1_1_0("HOUSHOLD", "600000", "4000000");
///IFilter filter4 = new PropertyIsLikeFilter_FE1_1_0("STATE_NAME", "New*");
///
/// // SelectedStatesAndHousholds: Green
///OGCFilterCollection filterCollection1 = new OGCFilterCollection();
///filterCollection1.AddFilter(filter1);
///filterCollection1.AddFilter(filter2);
///OGCFilterCollection filterCollection2 = new OGCFilterCollection();
///filterCollection2.AddFilter(filter3);
///filterCollection1.AddFilterCollection(filterCollection2);
///filterCollection1.Junctor = OGCFilterCollection.JunctorEnum.Or;
///prov2.OGCFilter = filterCollection1;
///
/// // Like-Filter('New*'): Bisque
///prov3.OGCFilter = filter4;
///
/// // Layer Style
///layer1.Style.Fill = new SolidBrush(Color.IndianRed); // States
///layer2.Style.Fill = new SolidBrush(Color.Green); // SelectedStatesAndHousholds
///layer3.Style.Fill = new SolidBrush(Color.Bisque); // e.g. New York, New Jersey,...
///layer5.Style.Fill = new SolidBrush(Color.LightBlue);
///
/// // Labels
/// // Labels are collected when parsing the geometry. So there's just one 'GetFeature' call necessary.
/// // Otherwise (when calling twice for retrieving labels) there may be an inconsistent read...
/// // If a label property is set, the quick geometry option is automatically set to 'false'.
///prov3.Label = "STATE_NAME";
///SharpMap.Layers.LabelLayer layLabel = new SharpMap.Layers.LabelLayer("labels");
///layLabel.DataSource = prov3;
///layLabel.Enabled = true;
///layLabel.LabelColumn = prov3.Label;
///layLabel.Style = new SharpMap.Styles.LabelStyle();
///layLabel.Style.CollisionDetection = false;
///layLabel.Style.CollisionBuffer = new SizeF(5, 5);
///layLabel.Style.ForeColor = Color.Black;
///layLabel.Style.Font = new Font(FontFamily.GenericSerif, 10);
///layLabel.MaxVisible = 90;
///layLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
/// // Options
/// // Defaults: MultiGeometries: true, QuickGeometries: false, GetFeatureGETRequest: false
/// // Render with validation...
///prov1.QuickGeometries = false;
/// // Important when connecting to an UMN MapServer
///prov1.GetFeatureGETRequest = true;
/// // Ignore multi-geometries...
///prov1.MultiGeometries = false;
///
/// // Quick geometries
/// // We need this option for prov2 since we have not passed a featuretype namespace
///prov2.QuickGeometries = true;
///prov4.QuickGeometries = true;
///prov5.QuickGeometries = true;
///prov6.QuickGeometries = true;
///
///layer1.DataSource = prov1;
///layer2.DataSource = prov2;
///layer3.DataSource = prov3;
///layer4.DataSource = prov4;
///layer5.DataSource = prov5;
///layer6.DataSource = prov6;
///
///demoMap.Layers.Add(layer1);
///demoMap.Layers.Add(layer2);
///demoMap.Layers.Add(layer3);
///demoMap.Layers.Add(layer4);
///demoMap.Layers.Add(layer5);
///demoMap.Layers.Add(layer6);
///demoMap.Layers.Add(layLabel);
///
///demoMap.Center = new GeoAPI.Geometries.Coordinate(-74.0, 40.7);
///demoMap.Zoom = 10;
/// // Alternatively zoom closer
/// // demoMap.Zoom = 0.2;
/// // Render map
///this.mapImage1.Image = demoMap.GetMap();
/// </code>
///</example>
public partial class WFS : IProvider
{
#region Enumerations
/// <summary>
/// This enumeration consists of expressions denoting WFS versions.
/// </summary>
public enum WFSVersionEnum
{
/// <summary>
/// Version 1.0.0
/// </summary>
WFS1_0_0,
/// <summary>
/// Version 1.1.0
/// </summary>
WFS1_1_0
} ;
#endregion
#region Fields
// Info about the featuretype to query obtained from 'GetCapabilites' and 'DescribeFeatureType'
private readonly GeometryTypeEnum _geometryType = GeometryTypeEnum.Unknown;
private readonly string _getCapabilitiesUri;
private readonly HttpClientUtil _httpClientUtil = new HttpClientUtil();
private readonly IWFS_TextResources _textResources;
private readonly WFSVersionEnum _wfsVersion;
private bool _disposed;
private string _featureType;
private WfsFeatureTypeInfo _featureTypeInfo;
private IXPathQueryManager _featureTypeInfoQueryManager;
private bool _isOpen;
private FeatureDataTable _labelInfo;
private int[] _axisOrder;
/// <summary>
/// Tree used for fast query of data
/// </summary>
private ISpatialIndex<uint> _tree;
private string _nsPrefix;
// The type of geometry can be specified in case of unprecise information (e.g. 'GeometryAssociationType').
// It helps to accelerate the rendering process significantly.
#endregion
#region Properties
private bool _getFeatureGETRequest;
private string _label;
private bool _multiGeometries = true;
private IFilter _ogcFilter;
private bool _quickGeometries;
/// <summary>
/// This cache (obtained from an already instantiated dataprovider that retrieves a featuretype hosted by the same service)
/// helps to speed up gathering metadata. It caches the 'GetCapabilities' response.
/// </summary>
public IXPathQueryManager GetCapabilitiesCache
{
get { return _featureTypeInfoQueryManager; }
set { _featureTypeInfoQueryManager = value; }
}
/// <summary>
/// Gets feature metadata
/// </summary>
public WfsFeatureTypeInfo FeatureTypeInfo
{
get { return _featureTypeInfo; }
}
/// <summary>
/// Gets or sets a value indicating the axis order
/// </summary>
/// <remarks>
/// The axis order is an array of array offsets. It can be einter {0, 1} or {1, 0}.
/// <para/>If not set explictly, <see cref="AxisOrderRegistry"/> is asked for a value based on <see cref="SRID"/>.</remarks>
public int[] AxisOrder
{
get { return _axisOrder ?? new AxisOrderRegistry()[SRID.ToString(NumberFormatInfo.InvariantInfo)]; }
set
{
if (value != null)
{
if (value.Length != 2)
throw new ArgumentException("Axis order array must have 2 elements");
if (!((value[0] == 0 && value[1] == 1)||
(value[0] == 1 && value[1] == 0)))
throw new ArgumentException("Axis order array values must be 0 or 1");
if (value[0] + value[1] != 1)
throw new ArgumentException("Sum of values in axis order array must 1");
}
_axisOrder = value;
}
}
/// <summary>
/// Gets or sets a value indicating the spatial index factory
/// </summary>
public static ISpatialIndexFactory<uint> SpatialIndexFactory = new QuadTreeFactory();
/// <summary>
/// Gets or sets a value indicating whether extracting geometry information
/// from 'GetFeature' response shall be done quickly without paying attention to
/// context validation, polygon boundaries and multi-geometries.
/// This option accelerates the geometry parsing process,
/// but in scarce cases can lead to errors.
/// </summary>
public bool QuickGeometries
{
get { return _quickGeometries; }
set { _quickGeometries = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the 'GetFeature' parser
/// should ignore multi-geometries (MultiPoint, MultiLineString, MultiCurve, MultiPolygon, MultiSurface).
/// By default it does not. Ignoring multi-geometries can lead to a better performance.
/// </summary>
public bool MultiGeometries
{
get { return _multiGeometries; }
set { _multiGeometries = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the 'GetFeature' request
/// should be done with HTTP GET. This option can be important when obtaining
/// data from a WFS provided by an UMN MapServer.
/// </summary>
public bool GetFeatureGETRequest
{
get { return _getFeatureGETRequest; }
set { _getFeatureGETRequest = value; }
}
/// <summary>
/// Gets or sets an OGC Filter.
/// </summary>
public IFilter OGCFilter
{
get { return _ogcFilter; }
set { _ogcFilter = value; }
}
/// <summary>
/// Gets or sets the property of the featuretype responsible for labels
/// </summary>
public string Label
{
get { return _label; }
set { _label = value; }
}
/// <summary>
/// Gets or sets the network credentials used for authenticating the request with the Internet resource
/// </summary>
public ICredentials Credentials
{
get { return _httpClientUtil.Credentials; }
set { _httpClientUtil.Credentials = value; }
}
/// <summary>
/// Gets and sets the proxy Url of the request.
/// </summary>
public string ProxyUrl
{
get { return _httpClientUtil.ProxyUrl; }
set { _httpClientUtil.ProxyUrl = value; }
}
#endregion
#region Constructors
/// <summary>
/// Use this constructor for initializing this dataprovider with all necessary
/// parameters to gather metadata from 'GetCapabilities' contract.
/// </summary>
/// <param name="getCapabilitiesURI">The URL for the 'GetCapabilities' request.</param>
/// <param name="nsPrefix">
/// Use an empty string or 'null', if there is no prefix for the featuretype.
/// </param>
/// <param name="featureType">The name of the feature type</param>
/// <param name="geometryType">
/// Specifying the geometry type helps to accelerate the rendering process,
/// if the geometry type in 'DescribeFeatureType is unprecise.
/// </param>
/// <param name="proxyUrl">Optional Proxy url</param>
/// <param name="wfsVersion">The desired WFS Server version.</param>
public WFS(string getCapabilitiesURI, string nsPrefix, string featureType, GeometryTypeEnum geometryType,
WFSVersionEnum wfsVersion, string proxyUrl = null)
{
_getCapabilitiesUri = getCapabilitiesURI;
if (wfsVersion == WFSVersionEnum.WFS1_0_0)
_textResources = new WFS_1_0_0_TextResources();
else
_textResources = new WFS_1_1_0_TextResources();
_wfsVersion = wfsVersion;
if (string.IsNullOrEmpty(nsPrefix))
ResolveFeatureType(featureType);
else
{
_nsPrefix = nsPrefix;
_featureType = featureType;
}
_geometryType = geometryType;
ProxyUrl = proxyUrl;
GetFeatureTypeInfo();
}
/// <summary>
/// Use this constructor for initializing this dataprovider with all necessary
/// parameters to gather metadata from 'GetCapabilities' contract.
/// </summary>
/// <param name="getCapabilitiesURI">The URL for the 'GetCapabilities' request.</param>
/// <param name="nsPrefix">
/// Use an empty string or 'null', if there is no prefix for the featuretype.
/// </param>
/// <param name="featureType">The name of the feature type</param>
/// <param name="wfsVersion">The desired WFS Server version.</param>
public WFS(string getCapabilitiesURI, string nsPrefix, string featureType, WFSVersionEnum wfsVersion)
: this(getCapabilitiesURI, nsPrefix, featureType, GeometryTypeEnum.Unknown, wfsVersion)
{
}
/// <summary>
/// Use this constructor for initializing this dataprovider with a
/// <see cref="WfsFeatureTypeInfo"/> object,
/// so that 'GetCapabilities' and 'DescribeFeatureType' can be bypassed.
/// </summary>
public WFS(WfsFeatureTypeInfo featureTypeInfo, WFSVersionEnum wfsVersion)
{
_featureTypeInfo = featureTypeInfo;
if (wfsVersion == WFSVersionEnum.WFS1_0_0)
_textResources = new WFS_1_0_0_TextResources();
else _textResources = new WFS_1_1_0_TextResources();
_wfsVersion = wfsVersion;
}
/// <summary>
/// Use this constructor for initializing this dataprovider with all mandatory
/// metadata for retrieving a featuretype, so that 'GetCapabilities' and 'DescribeFeatureType' can be bypassed.
/// </summary>
/// <param name="serviceURI">The service URL</param>
/// <param name="nsPrefix">
/// Use an empty string or 'null', if there is no prefix for the featuretype.
/// </param>
/// <param name="featureTypeNamespace">
/// Use an empty string or 'null', if there is no namespace for the featuretype.
/// You don't need to know the namespace of the feature type, if you use the quick geometries option.
/// </param>
/// <param name="geometryName">
/// The name of the geometry.
/// </param>
/// <param name="geometryType">
/// Specifying the geometry type helps to accelerate the rendering process.
/// </param>
/// <param name="featureType">The name of the feature type</param>
/// <param name="wfsVersion">The desired WFS Server version.</param>
public WFS(string serviceURI, string nsPrefix, string featureTypeNamespace, string featureType,
string geometryName, GeometryTypeEnum geometryType, WFSVersionEnum wfsVersion)
{
_featureTypeInfo = new WfsFeatureTypeInfo(serviceURI, nsPrefix, featureTypeNamespace, featureType,
geometryName, geometryType);
if (wfsVersion == WFSVersionEnum.WFS1_0_0)
_textResources = new WFS_1_0_0_TextResources();
else _textResources = new WFS_1_1_0_TextResources();
_wfsVersion = wfsVersion;
}
/// <summary>
/// Use this constructor for initializing this dataprovider with all mandatory
/// metadata for retrieving a featuretype, so that 'GetCapabilities' and 'DescribeFeatureType' can be bypassed.
/// </summary>
/// <param name="serviceURI">The service URL</param>
/// <param name="nsPrefix">
/// Use an empty string or 'null', if there is no prefix for the featuretype.
/// </param>
/// <param name="featureTypeNamespace">
/// Use an empty string or 'null', if there is no namespace for the featuretype.
/// You don't need to know the namespace of the feature type, if you use the quick geometries option.
/// </param>
/// <param name="geometryName">The name of the geometry</param>
/// <param name="featureType">The name of the feature type</param>
/// <param name="wfsVersion">The desired WFS Server version.</param>
public WFS(string serviceURI, string nsPrefix, string featureTypeNamespace, string featureType,
string geometryName, WFSVersionEnum wfsVersion)
: this(
serviceURI, nsPrefix, featureTypeNamespace, featureType, geometryName, GeometryTypeEnum.Unknown,
wfsVersion)
{
}
/// <summary>
/// Use this constructor for initializing this dataprovider with all necessary
/// parameters to gather metadata from 'GetCapabilities' contract.
/// </summary>
/// <param name="getCapabilitiesCache">
/// This cache (obtained from an already instantiated dataprovider that retrieves a featuretype hosted by the same service)
/// helps to speed up gathering metadata. It caches the 'GetCapabilities' response.
///</param>
/// <param name="nsPrefix">
/// Use an empty string or 'null', if there is no prefix for the featuretype.
/// </param>
/// <param name="geometryType">
/// Specifying the geometry type helps to accelerate the rendering process,
/// if the geometry type in 'DescribeFeatureType is unprecise.
/// </param>
/// <param name="featureType">The name of the feature type</param>
/// <param name="wfsVersion">The desired WFS Server version.</param>
/// <param name="proxyUrl">Optional proxy url</param>
public WFS(IXPathQueryManager getCapabilitiesCache, string nsPrefix, string featureType,
GeometryTypeEnum geometryType, WFSVersionEnum wfsVersion, string proxyUrl = null)
{
_featureTypeInfoQueryManager = getCapabilitiesCache;
if (wfsVersion == WFSVersionEnum.WFS1_0_0)
_textResources = new WFS_1_0_0_TextResources();
else
_textResources = new WFS_1_1_0_TextResources();
_wfsVersion = wfsVersion;
if (string.IsNullOrEmpty(nsPrefix))
ResolveFeatureType(featureType);
else
{
_nsPrefix = nsPrefix;
_featureType = featureType;
}
_geometryType = geometryType;
ProxyUrl = proxyUrl;
GetFeatureTypeInfo();
}
/// <summary>
/// Use this constructor for initializing this dataprovider with all necessary
/// parameters to gather metadata from 'GetCapabilities' contract.
/// </summary>
/// <param name="getCapabilitiesCache">
/// This cache (obtained from an already instantiated dataprovider that retrieves a featuretype hosted by the same service)
/// helps to speed up gathering metadata. It caches the 'GetCapabilities' response.
///</param>
/// <param name="nsPrefix">
/// Use an empty string or 'null', if there is no prefix for the featuretype.
/// </param>
/// <param name="featureType">The name of the feature type</param>
/// <param name="wfsVersion">The desired WFS Server version.</param>
public WFS(IXPathQueryManager getCapabilitiesCache, string nsPrefix, string featureType,
WFSVersionEnum wfsVersion)
: this(getCapabilitiesCache, nsPrefix, featureType, GeometryTypeEnum.Unknown, wfsVersion)
{
}
#endregion
#region IProvider Member
/// <summary>
/// Gets the features within the specified <see cref="GeoAPI.Geometries.Envelope"/>
/// </summary>
/// <param name="bbox"></param>
/// <returns>Features within the specified <see cref="GeoAPI.Geometries.Envelope"/></returns>
public virtual Collection<IGeometry> GetGeometriesInView(Envelope bbox)
{
if (_featureTypeInfo == null)
return null;
// if cache is not enabled make a call to server with the provided bounding box
if (!UseCache || Label == null)
{
_tree = null;
return LoadGeometries(bbox);
}
// if cache is enabled but data is not downloaded then make a server call with an infinite envelope to download all the geometries
if (_labelInfo == null)
{
LoadGeometries(new Envelope(double.MinValue, double.MaxValue, double.MinValue, double.MaxValue));
// creates the spatial index
var extent = GetExtents();
_tree = SpatialIndexFactory.Create(extent, _labelInfo.Count,
_labelInfo.Rows
.Cast<FeatureDataRow>()
.Select((row, idx) => SpatialIndexFactory.Create((uint) idx, row.Geometry.EnvelopeInternal)));
}
// we then must filter the geometries locally
var ids = _tree.Search(bbox);
var coll = new Collection<IGeometry>();
for (var i = 0; i < ids.Count; i++)
{
var featureRow = (FeatureDataRow) _labelInfo.Rows[(int)ids[i]];
coll.Add(featureRow.Geometry);
}
return coll;
}
/// <summary>
/// Returns all objects whose <see cref="GeoAPI.Geometries.Envelope"/> intersects 'bbox'.
/// </summary>
/// <remarks>
/// This method is usually much faster than the QueryFeatures method, because intersection tests
/// are performed on objects simplified by their <see cref="GeoAPI.Geometries.Envelope"/>, and using the Spatial Index
/// </remarks>
/// <param name="bbox">Box that objects should intersect</param>
/// <returns></returns>
/// <exception cref="Exception">Thrown in any case</exception>
public virtual Collection<uint> GetObjectIDsInView(Envelope bbox)
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// Returns the geometry corresponding to the Object ID
/// </summary>
/// <param name="oid">Object ID</param>
/// <returns>geometry</returns>
/// <exception cref="Exception">Thrown in any case</exception>
public virtual IGeometry GetGeometryByID(uint oid)
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// Returns the data associated with all the geometries that are intersected by 'geom'
/// </summary>
/// <param name="geom">Geometry to intersect with</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public virtual void ExecuteIntersectionQuery(IGeometry geom, FeatureDataSet ds)
{
if (_labelInfo == null) return;
var table = _labelInfo.Clone();
if (_tree != null)
{
// use the index for fast query
var ids = _tree.Search(geom.EnvelopeInternal);
for (var i = 0; i < ids.Count; i++)
{
var featureRow = (FeatureDataRow)_labelInfo.Rows[(int)ids[i]];
var featureGeometry = featureRow.Geometry;
if (featureGeometry.Intersects(geom))
{
var newRow = (FeatureDataRow) table.Rows.Add(featureRow.ItemArray);
newRow.Geometry = featureGeometry;
}
}
}
else
{
for (var i = 0; i < _labelInfo.Rows.Count; i++)
{
var featureRow = (FeatureDataRow) _labelInfo.Rows[i];
var featureGeometry = featureRow.Geometry;
if (featureGeometry.Intersects(geom))
{
var newRow = (FeatureDataRow) table.Rows.Add(featureRow.ItemArray);
newRow.Geometry = featureGeometry;
}
}
}
ds.Tables.Add(table);
// Destroy internal reference if cache is disabled
if (!UseCache)
_labelInfo = null;
}
/// <summary>
/// Returns the data associated with all the geometries that are intersected by 'geom'
/// </summary>
/// <param name="box">Geometry to intersect with</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public virtual void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
if (_labelInfo == null) return;
var table = _labelInfo.Clone();
if (_tree != null)
{
// use the index for fast query
var ids = _tree.Search(box);
for (var i = 0; i < ids.Count; i++)
{
var featureRow = (FeatureDataRow)_labelInfo.Rows[(int)ids[i]];
var featureGeometry = featureRow.Geometry;
var newRow = (FeatureDataRow)table.Rows.Add(featureRow.ItemArray);
newRow.Geometry = featureGeometry;
}
}
else
{
// we must filter the geometries locally
for (var i = 0; i < _labelInfo.Rows.Count; i++)
{
var featureRow = (FeatureDataRow) _labelInfo.Rows[i];
var featureGeometry = featureRow.Geometry;
if (box.Intersects(featureGeometry.EnvelopeInternal))
{
var newRow = (FeatureDataRow) table.Rows.Add(featureRow.ItemArray);
newRow.Geometry = featureGeometry;
}
}
}
ds.Tables.Add(table);
// Destroy internal reference
if (!UseCache)
_labelInfo = null;
}
/// <summary>
/// Returns the number of features in the dataset
/// </summary>
/// <returns>number of features</returns>
/// <exception cref="Exception">Thrown in any case</exception>
public virtual int GetFeatureCount()
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// Returns a <see cref="SharpMap.Data.FeatureDataRow"/> based on a RowID
/// </summary>
/// <param name="rowId">The id of the row.</param>
/// <returns>datarow</returns>
/// <exception cref="Exception">Thrown in any case</exception>
public virtual FeatureDataRow GetFeature(uint rowId)
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// The <see cref="Envelope"/> of dataset
/// </summary>
/// <returns>The 2d extent of the layer</returns>
public virtual Envelope GetExtents()
{
if (!UseCache || _labelInfo == null || _labelInfo.Rows.Count == 0)
{
return new Envelope(new Coordinate(_featureTypeInfo.BBox._MinLong, _featureTypeInfo.BBox._MinLat),
new Coordinate(_featureTypeInfo.BBox._MaxLong, _featureTypeInfo.BBox._MaxLat));
}
// here we try to fix a problem that happens when the server provides an incorrect bounding box for the data
// we simply calculate the extent from all the geometries we got.
Envelope env = null;
for (var i = 0; i < _labelInfo.Rows.Count; i++)
{
var featureRow = (FeatureDataRow)_labelInfo.Rows[i];
var geom = featureRow.Geometry;
env = env == null ? geom.EnvelopeInternal : env.ExpandedBy(geom.EnvelopeInternal);
}
return env;
}
/// <summary>
/// Gets the service-qualified name of the featuretype.
/// The service-qualified name enables the differentiation between featuretypes
/// from different services with an equal qualified name and therefore can be
/// regarded as an ID for the featuretype.
/// </summary>
public string ConnectionID
{
get { return _featureTypeInfo.ServiceURI + "/" + _featureTypeInfo.QualifiedName; }
}
/// <summary>
/// Opens the datasource
/// </summary>
public virtual void Open()
{
_isOpen = true;
}
/// <summary>
/// Closes the datasource
/// </summary>
public virtual void Close()
{
_isOpen = false;
_httpClientUtil.Close();
}
/// <summary>
/// Returns true if the datasource is currently open
/// </summary>
public bool IsOpen
{
get { return _isOpen; }
}
/// <summary>
/// The spatial reference ID (CRS)
/// </summary>
public virtual int SRID
{
get { return Convert.ToInt32(_featureTypeInfo.SRID); }
set { _featureTypeInfo.SRID = value.ToString(); }
}
/// <summary>
/// Gets or sets a value indicating whether caching is enabled.
/// </summary>
/// <remarks>
/// When cache is enabled all geometries are downloaded from server depending on the OGC filter set,
/// and then cached on client to fullfill next requests.
/// </remarks>
public bool UseCache { get; set; }
#endregion
#region IDisposable Member
/// <summary>
/// Method to perform cleanup work
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Implementation of the Dispose patter
/// </summary>
/// <param name="disposing">Flag indicating if called from <see cref="Dispose()"/> or a finalizer.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_featureTypeInfoQueryManager = null;
_labelInfo = null;
_httpClientUtil.Close();
}
_disposed = true;
}
}
#endregion
#region Private Member
private Collection<IGeometry> LoadGeometries(Envelope bbox)
{
var geometryTypeString = _featureTypeInfo.Geometry._GeometryType;
GeometryFactory geomFactory = null;
if (UseCache)
{
// we want to download all the elements of the feature
_labelInfo = new FeatureDataTable();
foreach (var element in FeatureTypeInfo.Elements)
_labelInfo.Columns.Add(element.Name);
_quickGeometries = false;
}
else if (!string.IsNullOrEmpty(_label))
{
_labelInfo = new FeatureDataTable();
_labelInfo.Columns.Add(_label);
// Turn off quick geometries, if a label is applied...
_quickGeometries = false;
}
// Configuration for GetFeature request */
WFSClientHTTPConfigurator config = new WFSClientHTTPConfigurator(_textResources);
config.configureForWfsGetFeatureRequest(_httpClientUtil, _featureTypeInfo, _label, bbox, _ogcFilter,
_getFeatureGETRequest, UseCache);
try
{
Collection<IGeometry> geoms;
switch (geometryTypeString)
{
/* Primitive geometry elements */
// GML2
case "PointPropertyType":
geomFactory = new PointFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML2
case "LineStringPropertyType":
geomFactory = new LineStringFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML2
case "PolygonPropertyType":
geomFactory = new PolygonFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML3
case "CurvePropertyType":
geomFactory = new LineStringFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML3
case "SurfacePropertyType":
geomFactory = new PolygonFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
/* Aggregate geometry elements */
// GML2
case "MultiPointPropertyType":
if (_multiGeometries)
geomFactory = new MultiPointFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
else
geomFactory = new PointFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML2
case "MultiLineStringPropertyType":
if (_multiGeometries)
geomFactory = new MultiLineStringFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
else
geomFactory = new LineStringFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML2
case "MultiPolygonPropertyType":
if (_multiGeometries)
geomFactory = new MultiPolygonFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
else
geomFactory = new PolygonFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML3
case "MultiCurvePropertyType":
if (_multiGeometries)
geomFactory = new MultiLineStringFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
else
geomFactory = new LineStringFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// GML3
case "MultiSurfacePropertyType":
if (_multiGeometries)
geomFactory = new MultiPolygonFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
else
geomFactory = new PolygonFactory(_httpClientUtil, _featureTypeInfo, _labelInfo);
break;
// .e.g. 'gml:GeometryAssociationType' or 'GeometryPropertyType'
//It's better to set the geometry type manually, if it is known...
default:
geomFactory = new UnspecifiedGeometryFactory_WFS1_0_0_GML2(_httpClientUtil, _featureTypeInfo,
_multiGeometries, _quickGeometries,
_labelInfo);
geomFactory.AxisOrder = AxisOrder;
geoms = geomFactory.createGeometries();
return geoms;
}
geomFactory.AxisOrder = AxisOrder;
geoms = _quickGeometries
? geomFactory.createQuickGeometries(geometryTypeString)
: geomFactory.createGeometries();
return geoms;
}
// Free resources (net connection of geometry factory)
finally
{
if (geomFactory != null)
{
geomFactory.Dispose();
}
}
}
/// <summary>
/// This method gets metadata about the featuretype to query from 'GetCapabilities' and 'DescribeFeatureType'.
/// </summary>
private void GetFeatureTypeInfo()
{
try
{
_featureTypeInfo = new WfsFeatureTypeInfo();
WFSClientHTTPConfigurator config = new WFSClientHTTPConfigurator(_textResources);
_featureTypeInfo.Prefix = _nsPrefix;
_featureTypeInfo.Name = _featureType;
string featureQueryName = string.IsNullOrEmpty(_nsPrefix)
? _featureType
: _nsPrefix + ":" + _featureType;
/***************************/
/* GetCapabilities request /
/***************************/
if (_featureTypeInfoQueryManager == null)
{
/* Initialize IXPathQueryManager with configured HttpClientUtil */
_featureTypeInfoQueryManager =
new XPathQueryManager_CompiledExpressionsDecorator(new XPathQueryManager());
_featureTypeInfoQueryManager.SetDocumentToParse(
config.configureForWfsGetCapabilitiesRequest(_httpClientUtil, _getCapabilitiesUri));
/* Namespaces for XPath queries */
_featureTypeInfoQueryManager.AddNamespace(_textResources.NSWFSPREFIX, _textResources.NSWFS);
_featureTypeInfoQueryManager.AddNamespace(_textResources.NSOWSPREFIX, _textResources.NSOWS);
_featureTypeInfoQueryManager.AddNamespace(_textResources.NSXLINKPREFIX, _textResources.NSXLINK);
}
/* Service URI (for WFS GetFeature request) */
_featureTypeInfo.ServiceURI = _featureTypeInfoQueryManager.GetValueFromNode
(_featureTypeInfoQueryManager.Compile(_textResources.XPATH_GETFEATURERESOURCE));
/* If no GetFeature URI could be found, try GetCapabilities URI */
if (_featureTypeInfo.ServiceURI == null) _featureTypeInfo.ServiceURI = _getCapabilitiesUri;