forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWfsSample.cs
More file actions
164 lines (143 loc) · 8.49 KB
/
WfsSample.cs
File metadata and controls
164 lines (143 loc) · 8.49 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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Net;
using SharpMap;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Styles;
using SharpMap.Utilities.Wfs;
using Point = GeoAPI.Geometries.Coordinate;
namespace WinFormSamples.Samples
{
public static class WfsSample
{
public static Map InitializeMap(float angle)
{
try
{
// WARNING
// This sample needs the GeoServer WFS running on your local machine.
// It uses the GeoServer default sample data. Installing and starting it is all you need to do
// http://docs.codehaus.org/display/GEOS/Download
// Sample by Peter Robineau
const string getCapabilitiesURI = "http://localhost:8080/geoserver/wfs";
const string serviceURI = "http://localhost:8080/geoserver/wfs";
Map map = new Map(new Size(600, 600));
map.MinimumZoom = 0.005;
map.BackColor = Color.White;
VectorLayer layer1 = new VectorLayer("States");
VectorLayer layer2 = new VectorLayer("SelectedStatesAndHousholds");
VectorLayer layer3 = new VectorLayer("New Jersey");
VectorLayer layer4 = new VectorLayer("Roads");
VectorLayer layer5 = new VectorLayer("Landmarks");
VectorLayer layer6 = new 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 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";
LabelLayer layLabel = new LabelLayer("labels");
layLabel.DataSource = prov3;
layLabel.Enabled = true;
layLabel.LabelColumn = prov3.Label;
layLabel.Style = new 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 = 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;
map.Layers.Add(layer1);
map.Layers.Add(layer2);
map.Layers.Add(layer3);
map.Layers.Add(layer4);
map.Layers.Add(layer5);
map.Layers.Add(layer6);
map.Layers.Add(layLabel);
map.Center = new Point(-74.0, 40.7);
map.Zoom = 10;
// Alternatively zoom closer
// demoMap.Zoom = 0.2;
Matrix mat = new Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
return map;
}
catch (WebException ex)
{
if ((ex.Message.Contains("(502) Bad Gateway")) ||
(ex.Message.Contains("Unable to connect to the remote server")))
{
throw new Exception(
"The Wfs sample threw an exception. You probably need to install the GeoServer WFS to your local machine. You can get it from here: http://docs.codehaus.org/display/GEOS/Download. The exception message was: " +
ex.Message);
}
else throw;
}
}
}
}