-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fragment model #253
base: master
Are you sure you want to change the base?
Fragment model #253
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# (c) Nelen & Schuurmans. GPL licensed, see LICENSE.rst. | ||
|
||
import os | ||
from collections import OrderedDict | ||
|
||
try: | ||
from osgeo import ogr | ||
except ImportError: | ||
ogr = None | ||
|
||
from threedigrid.admin import exporter_constants as const | ||
from threedigrid.geo_utils import get_spatial_reference | ||
from threedigrid.numpy_utils import reshape_flat_array | ||
from threedigrid.orm.base.exporters import BaseOgrExporter | ||
|
||
|
||
class FragmentsOgrExporter(BaseOgrExporter): | ||
""" | ||
Exports to ogr formats. You need to set the driver explicitly | ||
before calling save() | ||
""" | ||
|
||
def __init__(self, fragments): | ||
""" | ||
:param fragments: fragments.models.Fragments instance | ||
""" | ||
self._fragments = fragments | ||
self.supported_drivers = { | ||
const.GEO_PACKAGE_DRIVER_NAME, | ||
const.SHP_DRIVER_NAME, | ||
const.GEOJSON_DRIVER_NAME, | ||
} | ||
self.driver = None | ||
|
||
def save(self, file_name, layer_name, target_epsg_code, **kwargs): | ||
""" | ||
save to file format specified by the driver, e.g. shapefile | ||
|
||
:param file_name: name of the outputfile | ||
:param fragment_data: dict of fragment data | ||
""" | ||
if self.driver is None: | ||
self.set_driver(extension=os.path.splitext(file_name)[1]) | ||
|
||
assert self.driver is not None | ||
|
||
sr = get_spatial_reference(target_epsg_code) | ||
|
||
self.del_datasource(file_name) | ||
data_source = self.driver.CreateDataSource(file_name) | ||
layer = data_source.CreateLayer( | ||
str(os.path.basename(file_name)), sr, ogr.wkbPolygon | ||
) | ||
|
||
fields = OrderedDict( | ||
[ | ||
("id", "int"), | ||
("node_id", "int"), | ||
] | ||
) | ||
|
||
for field_name, field_type in fields.items(): | ||
layer.CreateField( | ||
ogr.FieldDefn(field_name, const.OGR_FIELD_TYPE_MAP[field_type]) | ||
) | ||
_definition = layer.GetLayerDefn() | ||
|
||
for i in range(len(self._fragments.coords)): | ||
if self._fragments.id[i] == 0: | ||
continue # skip the dummy element | ||
feature = ogr.Feature(_definition) | ||
ring = ogr.Geometry(ogr.wkbLinearRing) | ||
polygon_points = reshape_flat_array(self._fragments.coords[i]).T | ||
for x in polygon_points: | ||
ring.AddPoint(x[0], x[1]) | ||
polygon = ogr.Geometry(ogr.wkbPolygon) | ||
polygon.AddGeometry(ring) | ||
feature.SetGeometry(polygon) | ||
self.set_field(feature, "id", "int", self._fragments.id[i]) | ||
self.set_field(feature, "node_id", "int", self._fragments.node_id[i]) | ||
layer.CreateFeature(feature) | ||
feature.Destroy() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from threedigrid.admin.fragments import exporters | ||
from threedigrid.orm.fields import ArrayField, PolygonArrayField | ||
from threedigrid.orm.models import Model | ||
|
||
|
||
class Fragments(Model): | ||
|
||
node_id = ArrayField(type=int) | ||
coords = PolygonArrayField() | ||
|
||
GPKG_DEFAULT_FIELD_MAP = { | ||
"id": "id", | ||
"node_id": "node_id", | ||
"coords": "the_geom", | ||
} | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self._exporters = [ | ||
exporters.FragmentsOgrExporter(self), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
from threedigrid.admin.breaches.models import Breaches | ||
from threedigrid.admin.crosssections.models import CrossSections | ||
from threedigrid.admin.fragments.models import Fragments | ||
from threedigrid.admin.h5py_datasource import H5pyGroup | ||
from threedigrid.admin.levees.models import Levees | ||
from threedigrid.admin.lines.models import Lines | ||
|
@@ -115,6 +116,12 @@ def lines(self): | |
self.datasource_class(self.h5py_file, "lines"), **self._grid_kwargs | ||
) | ||
|
||
@property | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wat gebeurd hier als je een oude gridadmin pakt zonder 'fragments' ? Moet er nog een check in dat het bestaat of vangen we dat al ergens af.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Goeie, hier zal ik een testje voor toevoegen. |
||
def fragments(self): | ||
return Fragments( | ||
self.datasource_class(self.h5py_file, "fragments"), **self._grid_kwargs | ||
) | ||
|
||
@property | ||
def cross_sections(self): | ||
return CrossSections( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -508,6 +508,20 @@ def export_pumps(self): | |
dest, indent=self._indent | ||
) | ||
|
||
def export_fragments(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moeten fragments (uiteindelijk) ook als geojson in de API terecht komen? Nb. kan prima als een follow-up, |
||
if not self.ga.fragments.id.size == 0: | ||
logger.info( | ||
"[*] Model {} does not have fragments, " | ||
"skipping export fragments...".format(self.ga.model_name) | ||
) | ||
return | ||
|
||
dest_ow = os.path.join(self._dest, "fragments" + self._extension) | ||
getattr( | ||
self.ga.fragments.reproject_to(self._epsg), | ||
self._export_method, | ||
)(dest_ow, indent=self._indent) | ||
|
||
def export_levees(self): | ||
""" | ||
writes shapefile of levees | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍