Example #1
0
 def __init__(self, fields, model=None, indent=None):
     if geojson is None:
         raise_import_exception('geojson')
     self.fields = fields
     if model:
         assert isinstance(model, Model)
     self._model = model
     self._indent = indent
Example #2
0
    def _to_shapely_geom(self, values):
        if shapely is None:
            raise_import_exception('shapely')

        points = []
        for i, coord in enumerate(values.T):
            point = Point(coord[0], coord[1])
            point.index = i  # the index is used in get_mask_by_geometry
            points.append(point)
        return points
Example #3
0
    def _to_shapely_geom(self, values):
        if shapely is None:
            raise_import_exception('shapely')

        polygons = []
        for i, coords in enumerate(values):
            polygon = asPolygon(coords.reshape((2, -1)).T)
            polygon.index = i  # the index is used in get_mask_by_geometry
            polygons.append(polygon)
        return polygons
Example #4
0
    def _to_shapely_geom(self, values):
        if shapely is None:
            raise_import_exception('shapely')

        multilines = []
        for i, coords in enumerate(values):
            line = asLineString(coords.reshape((2, -1)).T)
            line.index = i  # the index is used in get_mask_by_geometry
            multilines.append(line)
        return multilines
Example #5
0
    def _to_shapely_geom(self, values):
        if shapely is None:
            raise_import_exception('shapely')

        polygons = []
        for i, coord in enumerate(values.T):
            # convert the bbox bottom-left and upper-right into a polygon
            polygon = Polygon([(coord[0], coord[1]), (coord[0], coord[3]),
                               (coord[2], coord[3]), (coord[2], coord[1])])
            polygon.index = i  # the index is used in get_mask_by_geometry
            polygons.append(polygon)
        return polygons
Example #6
0
    def load_geoms(self):
        """
        load levee geometries originating
        from model DB
        """

        # works on premise of ogr install
        if ogr is None:
            raise_import_exception('ogr')

        if self._geoms:
            return
        for line_coords in self.coords:
            line = ogr.Geometry(ogr.wkbLineString)
            linepoints = reshape_flat_array(line_coords).T
            for x in linepoints:
                line.AddPoint(x[0], x[1])
            self._geoms.append(line)
Example #7
0
 def __init__(self):
     if ogr is None:
         raise_import_exception('ogr')
Example #8
0
    def save(self, file_name, line_data, target_epsg_code, **kwargs):
        """
        save to file format specified by the driver, e.g. shapefile

        :param file_name: name of the outputfile
        :param line_data: dict of line data
        """
        if self.driver is None:
            self.set_driver(extension=os.path.splitext(file_name)[1])

        assert self.driver is not None

        kcu_dict = KCUDescriptor()
        geomtype = 0
        sr = get_spatial_reference(target_epsg_code)

        geom_source = 'from_threedicore'
        if 'geom' in kwargs:
            geom_source = kwargs['geom']

        # shapely is needed for the LineString creation, check if installed
        if geom_source == 'from_spatialite' and shapely_geom is None:
            raise_import_exception('shapely')

        self.del_datasource(file_name)
        data_source = self.driver.CreateDataSource(file_name)
        layer = data_source.CreateLayer(
            str(os.path.basename(file_name)),
            sr,
            geomtype
        )
        fields = kwargs.get('fields', LINE_BASE_FIELDS_ALL)
        for field_name, field_type in six.iteritems(fields):
            layer.CreateField(ogr.FieldDefn(
                    str(field_name),
                    const.OGR_FIELD_TYPE_MAP[field_type])
            )
        _definition = layer.GetLayerDefn()

        node_a = line_data['line'][0]
        node_b = line_data['line'][1]
        for i in range(node_a.size):
            if line_data['id'][i] == 0:
                continue  # skip the dummy element
            if geom_source == 'from_threedicore':
                line = ogr.Geometry(ogr.wkbLineString)
                line.AddPoint(line_data['line_coords'][0][i],
                              line_data['line_coords'][1][i])
                line.AddPoint(line_data['line_coords'][2][i],
                              line_data['line_coords'][3][i])
            elif geom_source == 'from_spatialite':
                linepoints = line_data['line_geometries'][i].reshape(
                    2, -1).T.astype('float64')
                line_geom = shapely_geom.LineString(linepoints)
                line = ogr.CreateGeometryFromWkt(line_geom.wkt)

            feature = ogr.Feature(_definition)
            feature.SetGeometry(line)
            for field_name, field_type in six.iteritems(fields):
                fname = LINE_FIELD_NAME_MAP.get(field_name, field_name)
                if field_name == 'kcu_descr':
                    raw_value = ''
                    try:
                        raw_value = str(kcu_dict[int(line_data['kcu'][i])])
                    except KeyError:
                        pass
                elif field_name == 'node_a':
                    raw_value = node_a[i]
                elif field_name == 'node_b':
                    raw_value = node_b[i]
                else:
                    try:
                        raw_value = line_data[fname][i]
                    except IndexError:
                        raw_value = None

                self.set_field(feature, field_name, field_type, raw_value)

            layer.CreateFeature(feature)
            feature.Destroy()