示例#1
0
def impute_rasters(stand_id, savetime):
    # import here to avoid circular dependencies
    from trees.utils import terrain_zonal
    from trees.models import Stand
    from django.conf import settings

    try:
        stand_qs = Stand.objects.filter(id=stand_id)
        stand = stand_qs[0]
    except (Stand.DoesNotExist, IndexError):
        raise impute_rasters.retry()

    rproj = settings.TERRAIN_PROJ
    g1 = stand.geometry_final.transform(rproj, clone=True)

    # Either get cached zonal stats or generate it
    key = "terrain_zonal_%s" % (g1.wkt.__hash__())
    stats = cache.get(key)
    if stats is None:
        stats = terrain_zonal(g1)
        cache.set(key, stats, 60 * 60 * 24 * 365)
    elevation, slope, aspect, cost = stats

    # stuff might have changed, we dont want a wholesale update of all fields!
    # use the timestamp to make sure we don't clobber a more recent request
    stand_qs.filter(rast_savetime__lt=savetime).update(elevation=elevation,
                                                       slope=slope,
                                                       aspect=aspect,
                                                       cost=cost,
                                                       rast_savetime=savetime)

    stand.invalidate_cache()

    res = {
        'stand_id': stand_id,
        'elevation': elevation,
        'aspect': aspect,
        'slope': slope,
        'cost': cost
    }

    if None in [elevation, aspect, slope, cost]:
        raise Exception("At least one raster is NULL for this geometry. %s" %
                        res)
    return res
示例#2
0
def impute_rasters(stand_id, savetime):
    # import here to avoid circular dependencies
    from trees.utils import terrain_zonal
    from trees.models import Stand
    from django.conf import settings

    try:
        stand_qs = Stand.objects.filter(id=stand_id)
        stand = stand_qs[0]
    except (Stand.DoesNotExist, IndexError):
        raise impute_rasters.retry()

    rproj = settings.TERRAIN_PROJ
    g1 = stand.geometry_final.transform(rproj, clone=True)

    # Either get cached zonal stats or generate it
    key = "terrain_zonal_%s" % (g1.wkt.__hash__())
    stats = cache.get(key)
    if stats is None:
        stats = terrain_zonal(g1)
        cache.set(key, stats, 60 * 60 * 24 * 365)
    elevation, slope, aspect, cost = stats

    # stuff might have changed, we dont want a wholesale update of all fields!
    # use the timestamp to make sure we don't clobber a more recent request
    stand_qs.filter(rast_savetime__lt=savetime).update(
        elevation=elevation,
        slope=slope,
        aspect=aspect,
        cost=cost,
        rast_savetime=savetime )

    stand.invalidate_cache()

    res = {'stand_id': stand_id, 'elevation': elevation, 'aspect': aspect, 'slope': slope, 'cost': cost}

    if None in [elevation, aspect, slope, cost]:
        raise Exception("At least one raster is NULL for this geometry. %s" % res)
    return res
示例#3
0
import os
import sys
from django.core.management import setup_environ
thisdir = os.path.dirname(os.path.abspath(__file__))
appdir = os.path.realpath(os.path.join(thisdir, '..', '..'))
sys.path.append(appdir)
import settings
setup_environ(settings)
##############################e
from django.contrib.gis.gdal import DataSource
from trees.utils import terrain_zonal

if __name__ == "__main__":
    shp = os.path.join(thisdir, 'data', 'test_shapes2.shp')
    ds = DataSource(shp) 
    lyr = ds[0]
    for feat in lyr:
    	print feat['id'], terrain_zonal(feat.geom)


示例#4
0
import os
import sys
from django.core.management import setup_environ
thisdir = os.path.dirname(os.path.abspath(__file__))
appdir = os.path.realpath(os.path.join(thisdir, '..', '..'))
sys.path.append(appdir)
import settings
setup_environ(settings)
##############################e
from django.contrib.gis.gdal import DataSource
from trees.utils import terrain_zonal

if __name__ == "__main__":
    shp = os.path.join(thisdir, 'data', 'test_shapes2.shp')
    ds = DataSource(shp)
    lyr = ds[0]
    for feat in lyr:
        print feat['id'], terrain_zonal(feat.geom)