本文整理汇总了Python中safe.storage.core.read_layer函数的典型用法代码示例。如果您正苦于以下问题:Python read_layer函数的具体用法?Python read_layer怎么用?Python read_layer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_layer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_run
def test_run(self):
"""TestVolcanoPointPopulationFunction: Test running the IF."""
merapi_point_path = test_data_path('hazard', 'volcano_point.shp')
population_path = test_data_path(
'exposure', 'pop_binary_raster_20_20.asc')
merapi_point_layer = read_layer(merapi_point_path)
population_layer = read_layer(population_path)
impact_function = VolcanoPointPopulationFunction.instance()
# Run merapi point
impact_function.hazard = merapi_point_layer
impact_function.exposure = population_layer
impact_function.run()
impact_layer = impact_function.impact
# Check the question
expected_question = (
'In the event of a volcano point how many '
'people might be impacted')
message = 'The question should be %s, but it returns %s' % (
expected_question, impact_function.question)
self.assertEqual(expected_question, impact_function.question, message)
# Count by hand
expected_affected_population = 200
result = numpy.nansum(impact_layer.get_data())
message = 'Expecting %s, but it returns %s' % (
expected_affected_population, result)
self.assertEqual(expected_affected_population, result, message)
示例2: test_run
def test_run(self):
function = FloodEvacuationRasterHazardFunction.instance()
hazard_path = test_data_path('hazard', 'continuous_flood_20_20.asc')
exposure_path = test_data_path(
'exposure', 'pop_binary_raster_20_20.asc')
hazard_layer = read_layer(hazard_path)
exposure_layer = read_layer(exposure_path)
function.hazard = hazard_layer
function.exposure = exposure_layer
function.run()
impact = function.impact
# Count of flooded objects is calculated "by the hands"
# print "keywords", keywords
keywords = impact.get_keywords()
evacuated = float(keywords['evacuated'])
total_needs_full = keywords['total_needs']
total_needs_weekly = OrderedDict([
[x['table name'], x['amount']] for x in
total_needs_full['weekly']
])
total_needs_single = OrderedDict([
[x['table name'], x['amount']] for x in
total_needs_full['single']
])
expected_evacuated = 100
self.assertEqual(evacuated, expected_evacuated)
self.assertEqual(total_needs_weekly['Rice [kg]'], 280)
self.assertEqual(total_needs_weekly['Family Kits'], 20)
self.assertEqual(total_needs_weekly['Drinking Water [l]'], 1750)
self.assertEqual(total_needs_weekly['Clean Water [l]'], 6700)
self.assertEqual(total_needs_single['Toilets'], 5)
示例3: test_flood_raster_building_impact_function
def test_flood_raster_building_impact_function(self):
"""Flood raster building impact function works
This test also exercises interpolation of hazard level (raster) to
building locations (vector data).
"""
for haz_filename in ['Flood_Current_Depth_Jakarta_geographic.asc',
'Flood_Design_Depth_Jakarta_geographic.asc']:
# Name file names for hazard level and exposure
hazard_filename = '%s/%s' % (HAZDATA, haz_filename)
exposure_filename = ('%s/OSM_building_polygons_20110905.shp'
% TESTDATA)
# Calculate impact using API
hazard_layer = read_layer(hazard_filename)
exposure_layer = read_layer(exposure_filename)
plugin_name = 'FloodRasterBuildingImpactFunction'
impact_function = get_plugin(plugin_name)
impact_vector = calculate_impact(
layers=[hazard_layer, exposure_layer],
impact_fcn=impact_function)
# Extract calculated result
icoordinates = impact_vector.get_geometry()
iattributes = impact_vector.get_data()
# Check
assert len(icoordinates) == 34960
assert len(iattributes) == 34960
示例4: test_run
def test_run(self):
impact_function = TsunamiRasterBuildingFunction.instance()
hazard_path = test_data_path("hazard", "continuous_flood_20_20.asc")
exposure_path = test_data_path("exposure", "buildings.shp")
hazard_layer = read_layer(hazard_path)
exposure_layer = read_layer(exposure_path)
impact_function.hazard = SafeLayer(hazard_layer)
impact_function.exposure = SafeLayer(exposure_layer)
impact_function.run()
impact_layer = impact_function.impact
# Extract calculated result
impact_data = impact_layer.get_data()
self.assertEqual(len(impact_data), 181)
# 1 = inundated, 2 = wet, 3 = dry
expected_result = {0: 1, 1: 116, 2: 64, 3: 0, 4: 0}
result = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
for feature in impact_data:
inundated_status = feature[impact_function.target_field]
result[inundated_status] += 1
message = "Expecting %s, but it returns %s" % (expected_result, result)
self.assertEqual(expected_result, result, message)
示例5: test_volcano_circle_population_impact
def test_volcano_circle_population_impact(self):
"""Volcano function runs circular evacuation zone."""
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = '%s/Merapi_alert.shp' % TESTDATA
exposure_filename = ('%s/glp10ag.asc' % EXPDATA)
# Calculate impact using API
hazard_layer = read_layer(hazard_filename)
exposure_layer = read_layer(exposure_filename)
plugin_name = 'Volcano Polygon Hazard Population'
impact_function = get_plugin(plugin_name)
impact_layer = calculate_impact(
layers=[hazard_layer, exposure_layer], impact_fcn=impact_function)
impact_filename = impact_layer.get_filename()
impact_layer = read_layer(impact_filename)
keywords = impact_layer.get_keywords()
print keywords
# This is the expected number of people affected
# Distance [km] Total Cumulative
# 3 15.800 15.800
# 5 17.300 33.100
# 10 125.000 158.000
message = 'Result not as expected'
impact_summary = keywords['impact_summary']
self.assertTrue(format_int(15800) in impact_summary, message)
self.assertTrue(format_int(17300) in impact_summary, message)
self.assertTrue(format_int(125000) in impact_summary, message)
示例6: test_run
def test_run(self):
function = AshRasterPopulationFunction.instance()
hazard_path = standard_data_path('hazard', 'ash_raster_wgs84.tif')
exposure_path = standard_data_path(
'exposure', 'pop_binary_raster_20_20.asc')
# We need clipping for both layers to be in the same dimension
clipped_hazard, clipped_exposure = clip_layers(
hazard_path, exposure_path)
hazard_layer = read_layer(clipped_hazard.source())
exposure_layer = read_layer(clipped_exposure.source())
# Let's set the extent to the hazard extent
function.hazard = SafeLayer(hazard_layer)
function.exposure = SafeLayer(exposure_layer)
function.run()
impact = function.impact
expected = [
[u'Population in very low hazard zone', 0],
[u'Population in medium hazard zone', 1374],
[u'Population in high hazard zone', 20],
[u'Population in very high hazard zone', 0],
[u'Population in low hazard zone', 8443],
[u'Total affected population', 9837],
[u'Unaffected population', 0],
[u'Total population', 9837],
[u'Population needing evacuation <sup>1</sup>', 9837]
]
self.assertListEqual(
expected, impact.impact_data['impact summary']['fields'])
示例7: test_run
def test_run(self):
function = ContinuousHazardPopulationFunction.instance()
hazard_path = test_data_path(
'hazard', 'continuous_flood_20_20.asc')
exposure_path = test_data_path(
'exposure', 'pop_binary_raster_20_20.asc')
hazard_layer = read_layer(hazard_path)
exposure_layer = read_layer(exposure_path)
function.hazard = SafeLayer(hazard_layer)
function.exposure = SafeLayer(exposure_layer)
function.run()
impact = function.impact
# print "keywords", keywords
keywords = impact.get_keywords()
total_needs_full = keywords['total_needs']
total_needs_weekly = OrderedDict([
[x['table name'], x['amount']] for x in
total_needs_full['weekly']
])
total_needs_single = OrderedDict([
[x['table name'], x['amount']] for x in
total_needs_full['single']
])
self.assertEqual(total_needs_weekly['Rice [kg]'], 336)
self.assertEqual(total_needs_weekly['Drinking Water [l]'], 2100)
self.assertEqual(total_needs_weekly['Clean Water [l]'], 8040)
self.assertEqual(total_needs_weekly['Family Kits'], 24)
self.assertEqual(total_needs_single['Toilets'], 6)
示例8: test_run
def test_run(self):
impact_function = FloodRasterBuildingFunction.instance()
hazard_path = test_data_path('hazard', 'continuous_flood_20_20.asc')
exposure_path = test_data_path('exposure', 'buildings.shp')
hazard_layer = read_layer(hazard_path)
exposure_layer = read_layer(exposure_path)
impact_function.hazard = hazard_layer
impact_function.exposure = exposure_layer
impact_function.run()
impact_layer = impact_function.impact
# Extract calculated result
impact_data = impact_layer.get_data()
self.assertEqual(len(impact_data), 181)
# 1 = inundated, 2 = wet, 3 = dry
expected_result = {
1: 64,
2: 117,
3: 0
}
result = {
1: 0,
2: 0,
3: 0
}
for feature in impact_data:
inundated_status = feature['INUNDATED']
result[inundated_status] += 1
message = 'Expecting %s, but it returns %s' % (expected_result, result)
self.assertEqual(expected_result, result, message)
示例9: test_run
def test_run(self):
function = ClassifiedRasterHazardBuildingFunction.instance()
hazard_path = standard_data_path(
'hazard', 'classified_hazard.tif')
exposure_path = standard_data_path('exposure', 'small_building.shp')
hazard_layer = read_layer(hazard_path)
exposure_layer = read_layer(exposure_path)
function.hazard = SafeLayer(hazard_layer)
function.exposure = SafeLayer(exposure_layer)
function.run_analysis()
impact_layer = function.impact
impact_data = impact_layer.get_data()
# Count
expected_impact = {
'Not affected': 5,
'Low hazard zone': 2,
'Medium hazard zone': 9,
'High hazard zone': 5
}
result_impact = {}
for impact_feature in impact_data:
hazard_class = impact_feature[function.target_field]
if hazard_class in result_impact:
result_impact[hazard_class] += 1
else:
result_impact[hazard_class] = 1
self.assertDictEqual(expected_impact, result_impact)
示例10: test_run
def test_run(self):
"""TestVolcanoPolygonBuildingFunction: Test running the IF."""
volcano_path = test_data_path('hazard', 'volcano_krb.shp')
building_path = test_data_path('exposure', 'buildings.shp')
hazard_layer = read_layer(volcano_path)
exposure_layer = read_layer(building_path)
impact_function = VolcanoPolygonBuildingFunction.instance()
impact_function.hazard = hazard_layer
impact_function.exposure = exposure_layer
impact_function.run()
impact_layer = impact_function.impact
# Check the question
expected_question = ('In the event of volcano krb how many buildings '
'might be affected')
message = 'The question should be %s, but it returns %s' % (
expected_question, impact_function.question)
self.assertEqual(expected_question, impact_function.question, message)
# The buildings should all be categorised into 5000 zone
zone_sum = impact_layer.get_data(attribute='zone')
krb3_zone_count = zone_sum.count('Kawasan Rawan Bencana III')
krb2_zone_count = zone_sum.count('Kawasan Rawan Bencana II')
# The result (counted by hand)
expected_krb3_count = 11
expected_krb2_count = 161
message = 'Expecting %s for KRB III zone, but it returns %s' % (
krb3_zone_count, expected_krb3_count)
self.assertEqual(krb3_zone_count, expected_krb3_count, message)
message = 'Expecting %s for KRB II zone, but it returns %s' % (
krb2_zone_count, expected_krb2_count)
self.assertEqual(krb2_zone_count, expected_krb2_count, message)
示例11: test_pager_earthquake_fatality_estimation
def test_pager_earthquake_fatality_estimation(self):
"""Fatalities from ground shaking can be computed correctly
using the Pager fatality model."""
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = '%s/itb_test_mmi.asc' % TESTDATA
exposure_filename = '%s/itb_test_pop.asc' % TESTDATA
# Calculate impact using API
hazard_layer = read_layer(hazard_filename)
exposure_layer = read_layer(exposure_filename)
plugin_name = 'PAG Fatality Function'
impact_function = get_plugin(plugin_name)
# Call calculation engine
impact_layer = calculate_impact(
layers=[hazard_layer, exposure_layer], impact_fcn=impact_function)
impact_filename = impact_layer.get_filename()
impact_layer = read_layer(impact_filename)
keywords = impact_layer.get_keywords()
population = keywords['total_population']
fatalities = keywords['total_fatalities']
# Check aggregated values
expected_population = 85425000.0
msg = ('Expected population was %f, I got %f'
% (expected_population, population))
self.assertEqual(population, expected_population, msg)
expected_fatalities = 410000.0
msg = ('Expected fatalities was %f, I got %f'
% (expected_fatalities, fatalities))
assert numpy.allclose(
fatalities, expected_fatalities, rtol=1.0e-5), msg
示例12: test_flood_vector_building_impact_function
def test_flood_vector_building_impact_function(self):
"""Test flood building impact function works (flood is polygon)."""
building = 'test_flood_building_impact_exposure.shp'
flood_data = 'test_flood_building_impact_hazard.shp'
plugin_name = 'FloodVectorBuildingImpactFunction'
hazard_filename = os.path.join(TESTDATA, flood_data)
exposure_filename = os.path.join(TESTDATA, building)
# Calculate impact using API
hazard_layer = read_layer(hazard_filename)
exposure_layer = read_layer(exposure_filename)
impact_function = get_plugin(plugin_name)
# Call calculation engine
impact_layer = calculate_impact(
layers=[hazard_layer, exposure_layer], impact_fcn=impact_function)
impact_filename = impact_layer.get_filename()
impact_layer = read_layer(impact_filename)
keywords = impact_layer.get_keywords()
buildings_total = keywords['buildings_total']
buildings_affected = keywords['buildings_affected']
self.assertEqual(buildings_total, 67)
self.assertEqual(buildings_affected, 41)
示例13: test_volcano_population_evacuation_impact
def test_volcano_population_evacuation_impact(self):
"""Population impact from volcanic hazard is computed correctly."""
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = '%s/donut.shp' % TESTDATA
exposure_filename = ('%s/pop_merapi_clip.tif' % TESTDATA)
# Calculate impact using API
hazard_layer = read_layer(hazard_filename)
exposure_layer = read_layer(exposure_filename)
plugin_name = 'Volcano Polygon Hazard Population'
impact_function = get_plugin(plugin_name)
impact_layer = calculate_impact(
layers=[hazard_layer, exposure_layer], impact_fcn=impact_function)
impact_filename = impact_layer.get_filename()
impact_layer = read_layer(impact_filename)
keywords = impact_layer.get_keywords()
# Check for expected results:
for value in ['Merapi', 192055, 56514, 68568, 66971]:
if isinstance(value, int):
x = format_int(population_rounding(value))
else:
x = value
summary = keywords['impact_summary']
msg = ('Did not find expected value %s in summary %s'
% (x, summary))
assert x in summary, msg
示例14: test_run
def test_run(self):
function = ClassifiedRasterHazardBuildingFunction.instance()
hazard_path = test_data_path('hazard', 'classified_flood_20_20.asc')
exposure_path = test_data_path('exposure', 'buildings.shp')
hazard_layer = read_layer(hazard_path)
exposure_layer = read_layer(exposure_path)
function.hazard = hazard_layer
function.exposure = exposure_layer
function.run()
impact_layer = function.impact
impact_data = impact_layer.get_data()
# Count
expected_impact = {
1.0: 67,
2.0: 49,
3.0: 64
}
result_impact = {
1.0: 0,
2.0: 0,
3.0: 0
}
for impact_feature in impact_data:
level = impact_feature['level']
if not math.isnan(level):
result_impact[level] += 1
message = 'Expecting %s, but it returns %s' % (
expected_impact, result_impact)
self.assertEqual(expected_impact, result_impact, message)
示例15: test_aggregate
def test_aggregate(self):
"""Aggregation by boundaries works
"""
# Name file names for hazard level and exposure
boundary_filename = ('%s/kecamatan_jakarta_osm.shp' % TESTDATA)
#data_filename = ('%s/Population_Jakarta_geographic.asc' % TESTDATA)
# Get reference building impact data
building_filename = ('%s/building_impact_scenario.shp' % TESTDATA)
boundary_layer = read_layer(boundary_filename)
building_layer = read_layer(building_filename)
res = aggregate(data=building_layer,
boundaries=boundary_layer,
attribute_name='AFFECTED',
aggregation_function='count')
#print res, len(res)
#print boundary_layer, len(boundary_layer)
msg = ('Number of aggregations %i should be the same as the '
'number of specified boundaries %i' % (len(res),
len(boundary_layer)))
assert len(res) == len(boundary_layer), msg