Skip to content

Commit 7af3e79

Browse files
committed
Add static Layer methods to create a Layer from a Geometry or a List of Geometries.
1 parent 3402b73 commit 7af3e79

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/main/groovy/geoscript/layer/Layer.groovy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,4 +1964,50 @@ class Layer implements Renderable {
19641964

19651965
outLayer
19661966
}
1967+
1968+
/**
1969+
* Create a Layer from a Geometry
1970+
* @param options Optional named parameters:
1971+
* <ul>
1972+
* <li>proj = The Projection (defaults to EPSg:4326)</li>
1973+
* <li>style = A Symbolizer</li>
1974+
* </ul>
1975+
* @param name The name of the new Layer
1976+
* @param geometry The Geometry
1977+
* @return The Layer
1978+
*/
1979+
static Layer fromGeometry(Map options = [:], String name, Geometry geometry) {
1980+
String geometryType = geometry.geometryType
1981+
String epsg = options.get("proj", new Projection("EPSG:4326")).epsg
1982+
Symbolizer symbolizer = options.get("style", Symbolizer.getDefault(geometryType))
1983+
Layer layer = new Layer(name, new Schema(name, "geom:${geometryType}:srid=${epsg}"))
1984+
layer.add([geom: geometry])
1985+
layer.style = symbolizer
1986+
layer
1987+
}
1988+
1989+
/**
1990+
* Create a Layer from a List of Geometries
1991+
* @param options Optional named parameters:
1992+
* <ul>
1993+
* <li>proj = The Projection (defaults to EPSg:4326)</li>
1994+
* <li>style = A Symbolizer</li>
1995+
* </ul>
1996+
* @param name The name of the new Layer
1997+
* @param geometries The List of Geometries
1998+
* @return The Layer
1999+
*/
2000+
static Layer fromGeometries(Map options = [:], String name, List<Geometry> geometries) {
2001+
List<String> geometryTypes = geometries.collect{ Geometry geometry -> geometry.geometryType}.unique()
2002+
String geometryType = geometryTypes.size() > 1 ? "GeometryCollection" : geometryTypes[0]
2003+
String epsg = options.get("proj", new Projection("EPSG:4326")).epsg
2004+
Symbolizer symbolizer = options.get("style", Symbolizer.getDefault(geometryType))
2005+
Layer layer = new Layer(name, new Schema(name, "geom:${geometryType}:srid=${epsg}"))
2006+
geometries.each { Geometry geometry ->
2007+
layer.add([geom: geometry])
2008+
}
2009+
layer.style = symbolizer
2010+
layer
2011+
}
2012+
19672013
}

src/test/groovy/geoscript/layer/LayerTestCase.groovy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package geoscript.layer
22

33
import geoscript.AssertUtil
44
import geoscript.filter.Expression
5+
import geoscript.style.Stroke
56
import geoscript.workspace.Directory
67
import groovy.json.JsonSlurper
78
import org.junit.Rule
@@ -39,6 +40,38 @@ class LayerTestCase {
3940
assertEquals "stations", layer4.name
4041
}
4142

43+
@Test void fromGeometry() {
44+
Layer layer = Layer.fromGeometry("world", new Bounds(-180,-90,180,90).geometry)
45+
assertEquals("world", layer.name)
46+
assertEquals(1, layer.count)
47+
assertEquals("EPSG:4326", layer.proj.id)
48+
49+
layer = Layer.fromGeometry("world", new Bounds(-180,-85,180,85, "EPSG:4326").reproject("EPSG:3857").geometry,
50+
proj: new Projection("EPSG:3857"),
51+
style: new Stroke("black", 0.50)
52+
)
53+
assertEquals("world", layer.name)
54+
assertEquals(1, layer.count)
55+
assertEquals("EPSG:3857", layer.proj.id)
56+
}
57+
58+
@Test void fromGeometries() {
59+
Layer layer = Layer.fromGeometries("testPits", Geometry.createRandomPoints(new Bounds(-180,-90,180,90).geometry, 10).geometries)
60+
assertEquals("testPits", layer.name)
61+
assertEquals("Point", layer.schema.geom.typ)
62+
assertEquals(10, layer.count)
63+
assertEquals("EPSG:4326", layer.proj.id)
64+
65+
layer = Layer.fromGeometries("geometries", [new Point(1,2), new LineString([1,2],[3,4])],
66+
proj: new Projection("EPSG:4326"),
67+
style: new Stroke("blue", 1)
68+
)
69+
assertEquals("geometries", layer.name)
70+
assertEquals("GeometryCollection", layer.schema.geom.typ)
71+
assertEquals(2, layer.count)
72+
assertEquals("EPSG:4326", layer.proj.id)
73+
}
74+
4275
@Test void eachFeature() {
4376
Layer layer = new Shapefile(new File(getClass().getClassLoader().getResource("states.shp").toURI()))
4477
int count = 0

0 commit comments

Comments
 (0)