Scripting GeoServer with
GeoScript
Justin Deoliveira, OpenGeo
GeoScript
Spatial capabilities for scripting languages.
GeoTools
Library providing tools for geospatial data.
GeoServer
Application server for publishing, editing, and
processing spatial data.
GeoServer Scripting Extension
Bringing the convenience of GeoScript together
with the power of GeoServer.
JSR-223
Scripting Hooks
Scripting Hooks
WPS Process
WFS Transaction Callback
HTTP Endpoint
Filter Function
Output Format *
Data Format *
* In progress
Demos
Read PostGIS Features - Java
...
import org.geotools.data.DataStore;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
...
Map<String,Serializable> params = new HashMap<String, Serializable>();
params.put(JDBCDataStoreFactory.HOST.key, "localhost");
params.put(JDBCDataStoreFactory.PORT.key, 5432);
params.put(JDBCDataStoreFactory.DATABASE.key, "geoscript");
params.put(JDBCDataStoreFactory.DBTYPE.key, "postgis");
params.put(JDBCDataStoreFactory.USER.key, "jdeolive");
PostgisNGDataStoreFactory factory = new PostgisNGDataStoreFactory();
DataStore pg = factory.createDataStore(params);
FeatureCollection features = pg.getFeatureSource("states").getFeatures();
FeatureIterator it = features.features();
try {
while(it.hasNext()) {
Feature f = it.next();
System.out.println(f.getProperty("STATE_NAME").getValue());
}
}
finally {
it.close();
}
Read PostGIS Features - Python
from geoscript.workspace import PostGIS
pg = PostGIS('geoscript')
for f in pg['states'].features():
print f['STATE_NAME']
Read PostGIS Features - JavaScript
var ws = require("geoscript/workspace");
var pg = ws.PostGIS("geoscript");
for (var f in pg.get("states").features) {
print(f.get("STATE_NAME"));
}
Geometry
>>> from geoscript import geom
>>> geom.Point(30, 10)
POINT(30 10)
>>> poly = geom.Point(30,10).buffer(5)
>>> poly.area
78.03612880645133
Geometry - I/O
>>> from geoscript import geom
>>> point = geom.Point(30, 10)
>>> geom.writeKML(point)
<kml:Point
xmlns:kml="http://earth.google.com/kml/2.1">
<kml:coordinates>0.0,0.0</kml:coordinates>
</kml:Point>
>>> geom.writeJSON(point)
{"type":"Point","coordinates":[30,10]}
Geometry - Visualization
js> var geom = require("geoscript/geom")
js> require("geoscript/viewer").bind()
js> var poly1 = geom.Point([0, 0]).buffer(1)
js> var poly2 = poly1.transform({dx: 0.5, dy: 0.5})
js> poly1.difference(poly2)
<Polygon [[[0.9095298326166407, -0.409529...>
Projection
js> var proj = require("geoscript/proj");
js> var p = proj.Projection("epsg:4326");
js> p.wkt
GEOGCS["WGS 84",
DATUM["World Geodetic System 1984",
...
Projection
>>> from geoscript import geom
>>> from geoscript.proj import Projection
>>> p = Projection('epsg:4326')
>>> p.transform((-111, 45.7), 'epsg:26912')
(500000.0, 5060716.313515949)
>>> g = geom.Point(0, 0).buffer(4)
>>> g = ...
>>> p.transform(g, 'epsg:26912')
>>> p.transform(g, 'epsg:3005')
WGS
84
UTM
Albers
Data Access - Workspace
>> from geoscript.workspace import PostGIS
>> pg = PostGIS('spearfish')
>> pg.layers()
['archsites', 'bugsites', ..., 'streams']
>> l = pg['archsites']
Data Access - Layers
>>> from geoscript.layer import Shapefile
>>> states = Shapefile('states.shp')
>>> states = states.reproject('epsg:3005')
Data Access - Features
js> var ws = require("geoscript/workspace")
js> var dir = ws.Directory("Data")
js> var states = dir.get("states")
js> for (var f in states.query("STATE_ABBR LIKE 'M%'")) {
>
print(f.get("STATE_NAME"));
> }
Maryland
Missouri
Mississippi
Michigan
...
Data Access - Rasters
>>> from geoscript.layer import GeoTIFF
>>> dem = GeoTIFF('sfdem.tif')
>>> dem.bands
[GRAY_INDEX]
>>> dem.extrema()
([-9.999999933815811e+36], [1840.0])
>>> h = dem.histogram(0, 1850)
Data Access - Plotting
>>> from geoscript.layer import GeoTIFF
>>> dem = GeoTIFF('sfdem.tif')
>>> h = dem.histogram(0, 1850)
>>>
>>>
>>>
>>>
from geoscript.plot import bar
y = h.counts()
x = map(lambda (x,y): y, h.bins())
bar.xy(zip(x, y)).show()
Styling - Stroke
>>> from geoscript.style import Stroke
>>> Stroke('#000000', width=2)
>>> Stroke('black', width=2, dash=[5,5])
>>> Stroke((0,0,0),width=2).hatch('vertline')
Styling - Shape and Icon
js> var style = require("geoscript/style");
js> style.Shape({name: "star", fill: "yellow"})
<Shape name: 'star', size: 6>
js> style.Icon("rainy.svg")
<Icon url: 'rainy.svg'>
Styling - Labels
>>> from geoscript.style import *
>>> font = 'bold 16px Arial'
>>> Shape() + Label('name',font)
.point(displace=(20,0))
>>> Stroke() + Label('name',font)
.linear(offset=10)
>>> Fill() + Label('name',font).halo('white',2)
Styling - Theming
>>> from geoscript.style *
>>> style = Stroke() + Label('STATE_ABBR', 14, 'Serif')
>>> style += Fill('#4DFF4D', 0.7).where('PERSONS < 2E6')
>>> style += Fill('#FF4D4D', 0.7).where('PERSONS BETWEEN 2E6 AND 4E6)
>>> style += Fill('#4D4DFF', 0.7).where('PERSONS > 4E6)
Process
>>> from geoscript.layer import Shapefile
>>> from geoscript.process import Process
>>> cities = Shapefile('world_cities.shp')
>>> p = Process.lookup('vec:Heatmap')
>>> p.run(cities, weightAttr='population')
Process
>>> p = Process.lookup('ras:PolygonExtraction')
>>> p.run(hm, range=(0.25, 0.85))
Thanks!