Example #1
0
dbhost = "localhost"
dbname = "dok_example"
dbuser = "******"
dbpass = "******"

# параметры поиска полигона в БД
geom_table = "crowns"
geom_ids = [55775, 25879, 29833, 23443, 57093]

# запрос в БД возвращающий полигон в формате WKT
wkt_geoms = []
for geom_id in geom_ids:
    SQL = """
    select ST_AsText(wkb_geometry)
    from {0}
    where ogc_fid = {1}
    """.format(geom_table, geom_id)
    _psql = psql(dbhost=dbhost, dbname=dbname, dbuser=dbuser, dbpass=dbpass)
    _psql.sql(SQL)
    wkt_geoms.append(_psql.fetchone()[0])
    _psql.close()

#print wkt_geoms

# вырезание области растра по полигону WKT
# и сохранение в формате стандартного словаря
std_dict = raster2array(in_file).cut_ogr_geometry(wkt_geoms)

# сохранеие стандартного словаря в растр
array2raster(None, std_dict, out_file)
Example #2
0
from rtools import raster2array, array2raster, raster2multi, repair2reload

valid_file = "valid.tif"
invalid_file = "invalid.tif"
repair_file = "repair.tif"

# test valid raster
raster_valid = raster2array(valid_file, 1)
print raster_valid.is_valid()

# test valid raster
raster_invalid = raster2array(invalid_file, 1)
print raster_invalid.is_valid()

# write new valid raster from invalid
array2raster(None, invalid_file.repair(), repair_file)

# repair invalid raster
repair2reload(invalid_file)
Example #3
0
import ogr

raster_file = "multiraster.tif"
shp_file = "points_array.shp"

# read from chennels
raster1 = raster2array(raster_file, 1)
raster2 = raster2array(raster_file, 2)
raster3 = raster2array(raster_file, 3)

# read shp file
_shp = ogr.Open(shp_file, update=1)
layer = _shp.GetLayerByIndex(0)

# add points for cut
point = []
for i in range(layer.GetFeatureCount()):
    geometry = layer.GetFeature(i)
    x, h, y, h = geometry.GetGeometryRef().GetEnvelope()
    point.append((float(x), float(y)))

# save cut to multi geotiff
raster2multi(
    "multi.tif",
    array2raster(raster1, raster1.cut_area(point)),
    array2raster(raster2, raster2.cut_area(point)),
    array2raster(raster3, raster3.cut_area(point)),
    # "MEM"
    # "GTiff"
)
Example #4
0
from rtools import raster2array, array2raster

raster_file = "raster.tif"
shp_file = "shp_cut/cut.shp"

raster = raster2array(raster_file, 1)
array2raster(None, raster.cut_shp_file(shp_file), "cut.tif")
Example #5
0
from osgeo import ogr
from rtools import raster2array, array2raster

ex_file = 'cut.tif'
cut_epsg = 4326
#cut_epsg = 28410
#cut_epsg = 32638
cut_file = 'forest.tif'
out_file = 'cut_extent.tif'

raster = raster2array(ex_file)
print raster.get_raster_extent(cut_epsg)
print raster.get_wgs84_area()
print raster.get_wgs84_area(pixel=True)
json = raster.get_raster_extent(cut_epsg, 'json')
wkt = raster.get_raster_extent(cut_epsg, 'wkt')
print wkt
print str(json)
ogr_geom = ogr.CreateGeometryFromJson(str(json))
print ogr_geom.ExportToWkt()

raster = raster2array(cut_file)
cut = raster.cut_ogr_geometry(wkt, 'wkt', cut_epsg)
array2raster(None, cut, out_file)