Skip to content

Commit 2c7dc38

Browse files
authored
Merge pull request #55 from geoscript/GEOSCRIPTPY-53
Add Geobuf Workspace
2 parents e741c03 + f60c843 commit 2c7dc38

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

geoscript/util/data.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from org.geotools.data.collection import ListFeatureCollection
2+
from org.geotools.feature.simple import SimpleFeatureBuilder
3+
24
def readFeatures(it, type, chunk):
35
i = 0
46
features = ListFeatureCollection(type)
@@ -7,3 +9,20 @@ def readFeatures(it, type, chunk):
79
i = i+1
810

911
return features
12+
13+
def readFeaturesWithChangedGeometry(it, sourceType, targetType, chunk):
14+
i = 0
15+
features = ListFeatureCollection(targetType)
16+
builder = SimpleFeatureBuilder(targetType)
17+
while it.hasNext() and i < chunk:
18+
f = it.next()
19+
for x in range(0, f.getAttributeCount()):
20+
name = f.getFeatureType().getDescriptor(x).getLocalName()
21+
value = f.getAttribute(x)
22+
if (name == sourceType.getGeometryDescriptor().getLocalName()):
23+
name = targetType.getGeometryDescriptor().getLocalName()
24+
builder.set(name, value)
25+
features.add(builder.buildFeature(f.getID()))
26+
i = i+1
27+
28+
return features

geoscript/workspace/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ def _import(mod, clas):
1717
Property = _import('property', 'Property')
1818
Oracle = _import('oracle', 'Oracle')
1919
GeoPackage = _import('geopackage', 'GeoPackage')
20+
Geobuf = _import('geobuf', 'Geobuf')
2021

geoscript/workspace/geobuf.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
the :mod:`workspace.property` module provides a workspace implemntation based on a directory of java style property files.
3+
"""
4+
5+
import os
6+
from java import io, net
7+
from geoscript import util
8+
from geoscript.workspace import Workspace
9+
from org.geotools.data.geobuf import GeobufDataStoreFactory
10+
11+
class Geobuf(Workspace):
12+
"""
13+
A subclass of :class:`Workspace <geoscript.workspace.workspace.Workspace>` that provides layers that correspond to geobuf files in a directory.
14+
15+
*dir* is the optional path as a ``str`` to a directory. If not specified it defaults to ``os.getcwd()``.
16+
"""
17+
18+
def __init__(self, dir=None, precision=6, dimension=2):
19+
dir = dir or os.getcwd()
20+
params = {'file': util.toFile(dir), 'precision': precision, 'dimension': dimension}
21+
Workspace.__init__(self, GeobufDataStoreFactory(), params)
22+
23+
def __repr__(self):
24+
return 'Geobuf[%s]' % str(self._store.info.source.path)

geoscript/workspace/workspace.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from org.geotools.data import DataStore
66
from geoscript.layer import Layer
77
from geoscript.filter import Filter
8-
from geoscript.util.data import readFeatures
8+
from geoscript.util.data import readFeatures, readFeaturesWithChangedGeometry
99
from geoscript import core, geom, feature
1010

1111
class Workspace:
@@ -168,7 +168,10 @@ def add(self, layer, name=None, chunk=1000):
168168
if features.isEmpty():
169169
break
170170

171-
l._source.addFeatures(features)
171+
if (l.schema.geom.name != layer.schema.geom.name):
172+
l._source.addFeatures(readFeaturesWithChangedGeometry(features.features(), layer._source.getSchema(), l._source.getSchema(), chunk))
173+
else:
174+
l._source.addFeatures(features)
172175

173176
return l
174177
finally:

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@
160160
<artifactId>gt-geopkg</artifactId>
161161
<version>${gt.version}</version>
162162
</dependency>
163+
<dependency>
164+
<groupId>org.geotools</groupId>
165+
<artifactId>gt-geobuf</artifactId>
166+
<version>${gt.version}</version>
167+
</dependency>
163168
<dependency>
164169
<groupId>org.python</groupId>
165170
<artifactId>jython-standalone</artifactId>

tests/workspace/test_geobuf.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
import shutil
3+
from tests.workspace.workspacetest import WorkspaceTest
4+
from geoscript.workspace import Geobuf
5+
6+
class GeobufWorkspace_Test(WorkspaceTest):
7+
8+
def setUp(self):
9+
self.path = 'work/geobufs'
10+
if os.path.isdir(self.path):
11+
shutil.rmtree(self.path)
12+
os.mkdir(self.path)
13+
self.ws = Geobuf(self.path)

0 commit comments

Comments
 (0)