Ejemplo n.º 1
0
def get_geometry(dataset):
    """
    Return ogr Geometry instance.
    """
    x1, a, b, y2, c, d = dataset.GetGeoTransform()
    x2 = x1 + a * dataset.RasterXSize + b * dataset.RasterYSize
    y1 = y2 + c * dataset.RasterXSize + d * dataset.RasterYSize

    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint_2D(x1, y1)
    ring.AddPoint_2D(x2, y1)
    ring.AddPoint_2D(x2, y2)
    ring.AddPoint_2D(x1, y2)
    ring.AddPoint_2D(x1, y1)
    geometry = ogr.Geometry(ogr.wkbPolygon)
    geometry.AddGeometry(ring)
    return geometry
Ejemplo n.º 2
0
def reindex(source_path, target_path, size):
    source_data_source = ogr.Open(source_path)
    source_layer = source_data_source[0]
    sr = source_layer.GetSpatialRef()
    checker = Checker(source_layer)
    extent = source_layer.GetExtent()

    target_data_source = driver.CreateDataSource(target_path)
    target_layer_name = os.path.basename(target_path)
    target_layer = target_data_source.CreateLayer(target_layer_name, sr)
    target_layer.CreateField(ogr.FieldDefn(str('name'), ogr.OFTString))
    target_layer_defn = target_layer.GetLayerDefn()

    xcorners = range(int(extent[0]), int(extent[2]), size)
    ycorners = range(int(extent[3]), int(extent[1]), -size)
    total = len(ycorners)
    chars = string.ascii_lowercase
    ogr.TermProgress_nocb(0)
    for m, y in enumerate(ycorners):
        for n, x in enumerate(xcorners):
            x1, y1, x2, y2 = x, y - size, x + size, y
            feature = ogr.Feature(target_layer_defn)
            xname = (chars[n // (26 * 26)] + chars[(n // 26) % 26] +
                     chars[n % 26] + '{:04.0f}'.format(n))
            feature[str('name')] = xname
            ring = ogr.Geometry(ogr.wkbLinearRing)
            ring.AddPoint_2D(x1, y1)
            ring.AddPoint_2D(x2, y1)
            ring.AddPoint_2D(x2, y2)
            ring.AddPoint_2D(x1, y2)
            ring.AddPoint_2D(x1, y1)
            geometry = ogr.Geometry(ogr.wkbPolygon)
            geometry.AddGeometry(ring)
            if not checker.intersects(geometry):
                continue
            geometry.AssignSpatialReference(sr)
            feature.SetGeometry(geometry)
            target_layer.CreateFeature(feature)
        ogr.TermProgress_nocb((m + 1) / total)
Ejemplo n.º 3
0
 def _convert(self, source_geometry):
     """
     Return converted linestring or multiline.
     """
     geometry_type = source_geometry.GetGeometryType()
     if geometry_type in LINESTRINGS:
         return self._convert_wkb_line_string(source_geometry)
     if geometry_type in MULTILINESTRINGS:
         target_geometry = ogr.Geometry(source_geometry.GetGeometryType())
         for source_wkb_line_string in source_geometry:
             target_geometry.AddGeometry(
                 self._convert_wkb_line_string(source_wkb_line_string), )
         return target_geometry
     raise ValueError('Unexpected geometry type: {}'.format(
         source_geometry.GetGeometryName(), ))
Ejemplo n.º 4
0
    def _convert_wkb_line_string(self, source_wkb_line_string):
        """ Return a wkb line string. """
        result = self._calculate(wkb_line_string=source_wkb_line_string)
        target_wkb_line_string = ogr.Geometry(ogr.wkbLineString)

        # add the first point of the first line
        result = self._calculate(wkb_line_string=source_wkb_line_string)
        (x, y), z = result['lines'][0, 0], result['values'][0]
        target_wkb_line_string.AddPoint(float(x), float(y), float(z))

        # add the centers (x, y) and values (z)
        for (x, y), z in zip(result['centers'], result['values']):
            target_wkb_line_string.AddPoint(float(x), float(y), float(z))

        # add the last point of the last line
        (x, y), z = result['lines'][-1, 1], result['values'][-1]
        target_wkb_line_string.AddPoint(float(x), float(y), float(z))

        return target_wkb_line_string
Ejemplo n.º 5
0
    def vectorize(self, index_feature):
        # target path
        name = index_feature[str('name')]
        path = os.path.join(self.target_path, name[:3], '{}'.format(name))
        if os.path.exists(path):
            return

        # create directory
        try:
            os.makedirs(os.path.dirname(path))
        except OSError:
            pass  # no problem

        index_geometry = index_feature.geometry()
        geo_transform = self.geo_transform.shifted(index_geometry)

        # data with one pixel margin on all sides
        indices = self.geo_transform.get_indices(index_geometry)
        indices = (indices[0] - 1, indices[1] - 1, indices[2] + 1,
                   indices[3] + 1)
        direction = self.direction_group.read(indices)
        accumulation = self.accumulation_group.read(indices)

        # processing
        data_source = SHAPE.CreateDataSource(str(path))
        layer_sr = osr.SpatialReference(self.projection)
        layer_name = str(os.path.basename(path))
        layer = data_source.CreateLayer(layer_name, layer_sr)
        layer.CreateField(ogr.FieldDefn(str('class'), ogr.OFTReal))
        layer_defn = layer.GetLayerDefn()
        generator = vectorize(direction=direction, accumulation=accumulation)
        for klass, indices in generator:
            feature = ogr.Feature(layer_defn)
            points = geo_transform.get_coordinates(indices)
            feature[str('class')] = klass
            geometry = ogr.Geometry(ogr.wkbLineString)
            for p in zip(*points):
                geometry.AddPoint_2D(*p)
            feature.SetGeometry(geometry)
            layer.CreateFeature(feature)