Skip to content

Commit dfbbfae

Browse files
committed
Add WFS Workspace example and a WMS example.
1 parent f0eb7b2 commit dfbbfae

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

examples/wfs.groovy

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* WFS is just another Workspace like a PostGIS database or a Directory full
3+
* of Shapefiles.
4+
*/
5+
6+
// Import the WFS Workspace
7+
import geoscript.workspace.*
8+
9+
// Other GeoScript Imports
10+
import geoscript.feature.Schema
11+
import geoscript.geom.Bounds
12+
import geoscript.filter.Filter
13+
14+
// Create a WFS Workspace with a GetCapabilities URL
15+
def wfs = new WFS("http://localhost:8080/geoserver/ows?service=wfs&version=1.1.0&request=GetCapabilities", timeout: 9000)
16+
17+
// Print the Layers
18+
println("Layers:")
19+
wfs.layers.each{layer ->
20+
println(layer)
21+
}
22+
23+
// Get a Layer and inspect some properties
24+
def layer = wfs.get("topp:states")
25+
println("Layer: ${layer.name}")
26+
println(" Format: ${layer.format}")
27+
println(" Projection: ${layer.proj}")
28+
println(" Schema: ${layer.schema}")
29+
println(" # Features: ${layer.count}")
30+
println(" Bounds: ${layer.bounds}")
31+
println(" Geometry Field: ${layer.schema.geom.name}")
32+
33+
// Print all features
34+
println(" Features:")
35+
layer.features.each{f->
36+
println("Feature:")
37+
println("----------------------")
38+
f.schema.fields.each {fld ->
39+
if (!fld.isGeometry()) println(" ${fld.name} = ${f.get(fld.name)}")
40+
}
41+
println("")
42+
}
43+
44+
// Query the Layer by attribute
45+
println("Features where STATE_ABBR = 'WA'")
46+
layer.getFeatures("STATE_ABBR = 'WA'").each{f ->
47+
println("Feature:")
48+
println("----------------------")
49+
f.schema.fields.each {fld ->
50+
if (!fld.isGeometry()) println(" ${fld.name} = ${f.get(fld.name)}")
51+
}
52+
println("")
53+
}
54+
55+
// Query the Layer by Bounding Box
56+
def filter = Filter.bbox("the_geom", new Bounds(43.636, -73.0442, 44.9487, -72.0159, "EPSG:4269"))
57+
println("Features for ${filter}")
58+
layer.getFeatures(filter).each{f ->
59+
println("Feature:")
60+
println("----------------------")
61+
f.schema.fields.each {fld ->
62+
if (!fld.isGeometry()) println(" ${fld.name} = ${f.get(fld.name)}")
63+
}
64+
println("")
65+
}
66+
67+
// Extract a subset from WFS and save it to a Shapefile
68+
def dir = new Directory(".")
69+
def schema = new Schema("wa_state", layer.schema.fields)
70+
def waLayer = dir.create(schema)
71+
def features = layer.getFeatures("STATE_ABBR = 'WA'")
72+
features.each{f->
73+
waLayer.add([
74+
the_geom: f.geom,
75+
STATE_ABBR: f.get('STATE_ABBR'),
76+
])
77+
}
78+
println("Shapefile: ${waLayer.name}")
79+
println(" Bounds: ${waLayer.bounds}")
80+
println(" Projection: ${waLayer.proj}")
81+
println(" Schema: ${waLayer.schema}")

examples/wms.groovy

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import geoscript.layer.*
2+
import geoscript.geom.Bounds
3+
import javax.imageio.ImageIO
4+
5+
// Create a connection to a WMS server
6+
WMS wms = new WMS("http://localhost:8080/geoserver/ows?service=wms&version=1.1.1&request=GetCapabilities")
7+
8+
// Print out some metadata
9+
println "Name: ${wms.name}"
10+
println "Title: ${wms.title}"
11+
println "Abstract: ${wms.abstract}"
12+
println "Keywords: ${wms.keywords.join(',')}"
13+
println "Online Resource: ${wms.onlineResource}"
14+
println "Update Sequence: ${wms.updateSequence}"
15+
println "Version: ${wms.version}"
16+
println "Formats: ${wms.getMapFormats.join(',')}"
17+
println "Layers:"
18+
wms.layers.each{layer ->
19+
println " Name: ${layer.name}"
20+
println " Title: ${layer.title}"
21+
println " Srs: ${layer.srses}"
22+
println " Styles: ${layer.styles}"
23+
layer.styles.each{style ->
24+
println " Name: ${style.name}"
25+
println " Title: ${style.title}"
26+
println " Abstract: ${style.abstract}"
27+
}
28+
println " Queryable: ${layer.queryable}"
29+
println " Max Scale: ${layer.maxScale}"
30+
println " Min Scale: ${layer.minScale}"
31+
println " Parent: ${layer.parent}"
32+
println " Children: ${layer.children}"
33+
println " Bounds: ${layer.bounds}"
34+
println " LatLon Bounds: ${layer.latLonBounds}"
35+
}
36+
// Get a WMS Layer
37+
println "Get Layer 'world:borders': ${wms.getLayer('world:borders').name}"
38+
39+
// Get an Image for a Layer
40+
def image = wms.getImage("world:borders")
41+
ImageIO.write(image,"png",new File("world.png"))
42+
43+
// Get an Image for Layers
44+
image = wms.getImage(["world:cities","world:borders"])
45+
ImageIO.write(image,"png",new File("world_cities.png"))
46+
47+
// Get an Image for Layers with custom styles
48+
image = wms.getImage([[name: "world:urbanareas1_1", style: "point"], [name: "world:urbanareas1_1", style: "heatmap"]])
49+
ImageIO.write(image,"png",new File("world_urbanareas.png"))
50+
51+
// Get an Image for Layers and a custom Bounds
52+
image = wms.getImage(["medford:hospitals","medford:citylimits"], bounds: new Bounds(-122.87999, 42.29600,-122.81312, 42.35004,"EPSG:4326"))
53+
ImageIO.write(image,"png",new File("medford.png"))
54+
55+
// Get a Raster for Layer
56+
def raster = wms.getRaster("world:borders")
57+
ImageIO.write(raster.image,"png",new File("raster_world.png"))
58+
59+
// Get a Raster for two Layers
60+
raster = wms.getRaster(["world:cities","world:borders"])
61+
ImageIO.write(raster.image,"png",new File("raster_world_cities.png"))
62+
63+
// Get a Raster for two Layers with custom styles
64+
raster = wms.getRaster([[name: "world:urbanareas1_1", style: "point"], [name: "world:urbanareas1_1", style: "heatmap"]])
65+
ImageIO.write(raster.image,"png",new File("raster_world_urbanareas.png"))
66+
67+
// Get a Raster for two Layers and a custom Bounds
68+
raster = wms.getRaster(["medford:hospitals","medford:citylimits"], bounds: new Bounds(-122.87999, 42.29600,-122.81312, 42.35004,"EPSG:4326"))
69+
ImageIO.write(raster.image,"png",new File("raster_medford.png"))
70+
71+
// Get a Legend
72+
def legend = wms.getLegend("world:borders")
73+
ImageIO.write(legend,"png",new File("legend_world.png"))
74+
75+
legend = wms.getLegend(wms.getLayer("world:cities"))
76+
ImageIO.write(legend,"png",new File("legend_cities.png"))
77+
78+
// Use WMSLayer in a Map
79+
def map = new geoscript.render.Map(
80+
layers: [new WMSLayer(wms, ["world:borders","world:cities"])]
81+
)
82+
map.render(new File("map_world.png"))
83+
84+
// Combine WMS with a Shapefile
85+
def states = new Shapefile("states.shp")
86+
map = new geoscript.render.Map(
87+
layers: [new WMSLayer(wms, ["world:borders"]),states]
88+
)
89+
map.render(new File("map_world_states.png"))

0 commit comments

Comments
 (0)