Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions geoscript/util/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from org.geotools.data.collection import ListFeatureCollection
from org.geotools.feature.simple import SimpleFeatureBuilder

def readFeatures(it, type, chunk):
i = 0
features = ListFeatureCollection(type)
Expand All @@ -7,3 +9,20 @@ def readFeatures(it, type, chunk):
i = i+1

return features

def readFeaturesWithChangedGeometry(it, sourceType, targetType, chunk):
i = 0
features = ListFeatureCollection(targetType)
builder = SimpleFeatureBuilder(targetType)
while it.hasNext() and i < chunk:
f = it.next()
for x in range(0, f.getAttributeCount()):
name = f.getFeatureType().getDescriptor(x).getLocalName()
value = f.getAttribute(x)
if (name == sourceType.getGeometryDescriptor().getLocalName()):
name = targetType.getGeometryDescriptor().getLocalName()
builder.set(name, value)
features.add(builder.buildFeature(f.getID()))
i = i+1

return features
1 change: 1 addition & 0 deletions geoscript/workspace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ def _import(mod, clas):
Property = _import('property', 'Property')
Oracle = _import('oracle', 'Oracle')
GeoPackage = _import('geopackage', 'GeoPackage')
Geobuf = _import('geobuf', 'Geobuf')

24 changes: 24 additions & 0 deletions geoscript/workspace/geobuf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
the :mod:`workspace.property` module provides a workspace implemntation based on a directory of java style property files.
"""

import os
from java import io, net
from geoscript import util
from geoscript.workspace import Workspace
from org.geotools.data.geobuf import GeobufDataStoreFactory

class Geobuf(Workspace):
"""
A subclass of :class:`Workspace <geoscript.workspace.workspace.Workspace>` that provides layers that correspond to geobuf files in a directory.

*dir* is the optional path as a ``str`` to a directory. If not specified it defaults to ``os.getcwd()``.
"""

def __init__(self, dir=None, precision=6, dimension=2):
dir = dir or os.getcwd()
params = {'file': util.toFile(dir), 'precision': precision, 'dimension': dimension}
Workspace.__init__(self, GeobufDataStoreFactory(), params)

def __repr__(self):
return 'Geobuf[%s]' % str(self._store.info.source.path)
7 changes: 5 additions & 2 deletions geoscript/workspace/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from org.geotools.data import DataStore
from geoscript.layer import Layer
from geoscript.filter import Filter
from geoscript.util.data import readFeatures
from geoscript.util.data import readFeatures, readFeaturesWithChangedGeometry
from geoscript import core, geom, feature

class Workspace:
Expand Down Expand Up @@ -168,7 +168,10 @@ def add(self, layer, name=None, chunk=1000):
if features.isEmpty():
break

l._source.addFeatures(features)
if (l.schema.geom.name != layer.schema.geom.name):
l._source.addFeatures(readFeaturesWithChangedGeometry(features.features(), layer._source.getSchema(), l._source.getSchema(), chunk))
else:
l._source.addFeatures(features)

return l
finally:
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@
<artifactId>gt-geopkg</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geobuf</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions tests/workspace/test_geobuf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
import shutil
from tests.workspace.workspacetest import WorkspaceTest
from geoscript.workspace import Geobuf

class GeobufWorkspace_Test(WorkspaceTest):

def setUp(self):
self.path = 'work/geobufs'
if os.path.isdir(self.path):
shutil.rmtree(self.path)
os.mkdir(self.path)
self.ws = Geobuf(self.path)