本文整理汇总了Python中safe_qgis.utilities.utilities_for_testing.load_layer函数的典型用法代码示例。如果您正苦于以下问题:Python load_layer函数的具体用法?Python load_layer怎么用?Python load_layer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load_layer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_zonal_with_exact_cell_boundaries
def test_zonal_with_exact_cell_boundaries(self):
"""Test that zonal stats returns the expected output."""
raster_layer, _ = load_layer(os.path.join(
UNITDATA, 'other', 'tenbytenraster.asc'))
# Note this is a matrix of 11x11 polygons - one per cell
# and one poly extending beyond to the right of each row
# and one poly extending beyond the bottom of each col
vector_layer, _ = load_layer(os.path.join(
UNITDATA, 'other', 'ten_by_ten_raster_as_polys.shp'))
result = calculate_zonal_stats(
raster_layer=raster_layer,
polygon_layer=vector_layer)
expected_result = {
0L: {'count': 1.0, 'sum': 0.0, 'mean': 0.0}, # TL polygon
9L: {'count': 1.0, 'sum': 9.0, 'mean': 9.0}, # TR polygon
25L: {'count': 1.0, 'sum': 3.0, 'mean': 3.0}, # Central polygon
88L: {'count': 1.0, 'sum': 0.0, 'mean': 0.0}, # BL polygon
108L: {'count': 1.0, 'sum': 9.0, 'mean': 9.0}} # BR polygon
# We will just check TL, TR, Middle, BL and BR cells
result = {
0L: result[0L],
9L: result[9L],
25L: result[25L],
88L: result[88L],
108L: result[108L]}
# noinspection PyPep8Naming
self.maxDiff = None
self.assertDictEqual(expected_result, result)
示例2: register_layers
def register_layers(self):
"""Register needed layers to QgsMapLayerRegistry."""
# Register 4 impact layers and aggregation layer
self.population_entire_jakarta_layer, _ = load_layer(
population_entire_jakarta_impact_path,
directory=None)
self.building_entire_jakarta_layer, _ = load_layer(
building_entire_jakarta_impact_path,
directory=None)
self.population_district_jakarta_layer, _ = load_layer(
population_district_jakarta_impact_path,
directory=None)
self.building_district_jakarta_layer, _ = load_layer(
building_district_jakarta_impact_path,
directory=None)
self.district_jakarta_layer, _ = load_layer(
district_jakarta_boundary_path,
directory=None)
layer_list = [self.population_entire_jakarta_layer,
self.population_district_jakarta_layer,
self.building_entire_jakarta_layer,
self.building_district_jakarta_layer,
self.district_jakarta_layer]
# noinspection PyArgumentList
self.map_layer_registry.addMapLayers(layer_list)
示例3: setUp
def setUp(self):
self.keyword_io = KeywordIO()
uri = QgsDataSourceURI()
uri.setDatabase(os.path.join(TESTDATA, 'jk.sqlite'))
uri.setDataSource('', 'osm_buildings', 'Geometry')
self.sqlite_layer = QgsVectorLayer(
uri.uri(), 'OSM Buildings', 'spatialite')
hazard_path = os.path.join(HAZDATA, 'Shakemap_Padang_2009.asc')
self.raster_layer, layer_type = load_layer(
hazard_path, directory=None)
del layer_type
self.vector_layer, layer_type = load_layer('Padang_WGS84.shp')
del layer_type
self.expected_sqlite_keywords = {
'category': 'exposure',
'datatype': 'OSM',
'subcategory': 'building'}
self.expected_vector_keywords = {
'category': 'exposure',
'datatype': 'itb',
'subcategory': 'structure',
'title': 'Padang WGS84'}
self.expected_raster_keywords = {
'category': 'hazard',
'source': 'USGS',
'subcategory': 'earthquake',
'unit': 'MMI',
'title': ('An earthquake in Padang '
'like in 2009')}
示例4: test_issue121
def test_issue121(self):
"""Test that point symbol size can be set from style (issue 121).
.. seealso:: https://github.com/AIFDR/inasafe/issues/121
"""
myLayer, myType = load_layer('kecamatan_jakarta_osm_centroids.shp')
del myType
# Note the float quantity values below
myStyleInfo = {'target_field': 'KEPADATAN', 'style_classes': [
{'opacity': 1, 'max': 200, 'colour': '#fecc5c',
'min': 45, 'label': 'Low', 'size': 1},
{'opacity': 1, 'max': 350, 'colour': '#fd8d3c',
'min': 201, 'label': 'Medium', 'size': 2},
{'opacity': 1, 'max': 539, 'colour': '#f31a1c',
'min': 351, 'label': 'High', 'size': 3}]}
myMessage = 'Setting style with point sizes should work.'
try:
set_vector_graduated_style(myLayer, myStyleInfo)
except:
raise Exception(myMessage)
# Now validate the size values were set as expected
# new symbology - subclass of QgsFeatureRendererV2 class
myRenderer = myLayer.rendererV2()
myType = myRenderer.type()
assert myType == 'graduatedSymbol'
mySize = 1
for myRange in myRenderer.ranges():
mySymbol = myRange.symbol()
mySymbolLayer = mySymbol.symbolLayer(0)
myActualSize = mySymbolLayer.size()
myMessage = (('Expected symbol layer 0 for range %s to have'
' a size of %s, got %s') %
(mySize, mySize, myActualSize))
assert mySize == myActualSize, myMessage
mySize += 1
示例5: test_transparency_of_minimum_value
def test_transparency_of_minimum_value(self):
"""Test that transparency of minimum value works when set to 100%
"""
# This dataset has all cells with value 1.3
myLayer, _ = load_layer('issue126.tif')
# Note the float quantity values below
myStyleInfo = {'style_classes': [
{'colour': '#FFFFFF', 'transparency': 100, 'quantity': 0.0},
{'colour': '#38A800', 'quantity': 0.038362596547925065,
'transparency': 0, 'label': u'Rendah [0 orang/sel]'},
{'colour': '#79C900', 'transparency': 0,
'quantity': 0.07672519309585013},
{'colour': '#CEED00', 'transparency': 0,
'quantity': 0.1150877896437752},
{'colour': '#FFCC00', 'quantity': 0.15345038619170026,
'transparency': 0, 'label': u'Sedang [0 orang/sel]'},
{'colour': '#FF6600', 'transparency': 0,
'quantity': 0.19181298273962533},
{'colour': '#FF0000', 'transparency': 0,
'quantity': 0.23017557928755039},
{'colour': '#7A0000', 'quantity': 0.26853817583547546,
'transparency': 0, 'label': u'Tinggi [0 orang/sel]'}]}
try:
setRasterStyle(myLayer, myStyleInfo)
except Exception, e:
myMessage = '\nCould not create raster style'
e.args = (e.args[0] + myMessage,)
raise
示例6: test_get_legend
def test_get_legend(self):
"""Getting a legend for a generic layer works."""
LOGGER.debug('test_get_legend called')
layer, _ = load_layer('test_shakeimpact.shp')
map_legend = MapLegend(layer)
self.assertTrue(map_legend.layer is not None)
legend = map_legend.get_legend()
path = unique_filename(
prefix='getLegend',
suffix='.png',
dir=temp_dir('test'))
legend.save(path, 'PNG')
LOGGER.debug(path)
# As we have discovered, different versions of Qt and
# OS platforms cause different output, so myControlImages is a list
# of 'known good' renders.
tolerance = 0 # to allow for version number changes in disclaimer
flag, message = check_images('getLegend', path, tolerance)
message += (
'\nWe want these images to match, if they do already '
'copy the test image generated to create a new control '
'image.')
self.assertTrue(flag, message)
LOGGER.debug('test_getLegend done')
示例7: test_add_symbol_to_legend
def test_add_symbol_to_legend(self):
"""Test we can add a symbol to the legend."""
layer, _ = load_layer('test_floodimpact.tif')
map_legend = MapLegend(layer)
symbol = QgsFillSymbolV2()
symbol.setColor(QtGui.QColor(12, 34, 56))
map_legend.add_symbol(
symbol,
minimum=0,
# expect 2.0303 in legend
maximum=2.02030,
label='Foo')
path = unique_filename(
prefix='addSymbolToLegend',
suffix='.png',
dir=temp_dir('test'))
map_legend.get_legend().save(path, 'PNG')
LOGGER.debug(path)
# As we have discovered, different versions of Qt and
# OS platforms cause different output, so myControlImages is a list
# of 'known good' renders.
tolerance = 0 # to allow for version number changes in disclaimer
flag, message = check_images(
'addSymbolToLegend', path, tolerance)
message += (
'\nWe want these images to match, if they do already, copy the '
'test image generated to create a new control image.')
self.assertTrue(flag, message)
示例8: test_addSymbolToLegend
def test_addSymbolToLegend(self):
"""Test we can add a symbol to the legend."""
myLayer, _ = load_layer('test_floodimpact.tif')
myMapLegend = MapLegend(myLayer)
mySymbol = QgsSymbol()
mySymbol.setColor(QtGui.QColor(12, 34, 56))
myMapLegend.addSymbolToLegend(
mySymbol,
theMin=0,
# expect 2.0303 in legend
theMax=2.02030,
theLabel='Foo')
myPath = unique_filename(
prefix='addSymbolToLegend',
suffix='.png',
dir=temp_dir('test'))
myMapLegend.getLegend().save(myPath, 'PNG')
LOGGER.debug(myPath)
# As we have discovered, different versions of Qt and
# OS platforms cause different output, so myControlImages is a list
# of 'known good' renders.
myTolerance = 0 # to allow for version number changes in disclaimer
myFlag, myMessage = check_images('addSymbolToLegend', myPath,
myTolerance)
myMessage += ('\nWe want these images to match, if they do already '
'copy the test image generated to create a new control '
'image.')
assert myFlag, myMessage
示例9: test_getVectorLegend
def test_getVectorLegend(self):
"""Getting a legend for a vector layer works.
@note This test is not related do thousand separator since we insert
our own legend notes and our own layer.
"""
myLayer, _ = load_layer('test_shakeimpact.shp')
myMapLegend = MapLegend(
myLayer,
theLegendNotes='Thousand separator represented by \',\'',
theLegendUnits='(people per cell)')
myImage = myMapLegend.getVectorLegend()
myPath = unique_filename(
prefix='getVectorLegend',
suffix='.png',
dir=temp_dir('test'))
myImage.save(myPath, 'PNG')
LOGGER.debug(myPath)
# As we have discovered, different versions of Qt and
# OS platforms cause different output, so myControlImages is a list
# of 'known good' renders.
myTolerance = 0 # to allow for version number changes in disclaimer
myFlag, myMessage = check_images(
'getVectorLegend', myPath, myTolerance)
myMessage += ('\nWe want these images to match, if they do already '
'copy the test image generated to create a new control '
'image.')
assert myFlag, myMessage
示例10: test_defaultTemplate
def test_defaultTemplate(self):
"""Test that loading default template works"""
LOGGER.info('Testing defaultTemplate')
myLayer, _ = load_layer('test_shakeimpact.shp')
myCanvasLayer = QgsMapCanvasLayer(myLayer)
CANVAS.setLayerSet([myCanvasLayer])
myRect = QgsRectangle(106.7894, -6.2308, 106.8004, -6.2264)
CANVAS.setExtent(myRect)
CANVAS.refresh()
myMap = Map(IFACE)
myMap.set_impact_layer(myLayer)
myPath = unique_filename(prefix='mapDefaultTemplateTest',
suffix='.pdf',
dir=temp_dir('test'))
myMap.make_pdf(myPath)
LOGGER.debug(myPath)
myMessage = 'Rendered output does not exist: %s' % myPath
assert os.path.exists(myPath), myMessage
# pdf rendering is non deterministic so we can't do a hash check
# test_renderComposition renders just the image instead of pdf
# so we hash check there and here we just do a basic minimum file
# size check.
mySize = os.stat(myPath).st_size
# Note: You should replace, not append the numbers for a given
# platform. Also note that this test will break every time the
# version number of InaSAFE changes so we should ultimately come up
# with a lower maintenance test strategy.
myExpectedSizes = [
400350, # Slackware64 14.0
]
myMessage = '%s\nExpected rendered map pdf to be in %s, got %s' % (
myPath, myExpectedSizes, mySize)
self.assertIn(mySize, myExpectedSizes, myMessage)
示例11: test_issue_121
def test_issue_121(self):
"""Test that point symbol size can be set from style (issue 121).
.. seealso:: https://github.com/AIFDR/inasafe/issues/121
"""
layer, layer_type = load_layer('kecamatan_jakarta_osm_centroids.shp')
del layer_type
# Note the float quantity values below
style_info = {'target_field': 'KEPADATAN', 'style_classes': [
{'opacity': 1, 'max': 200, 'colour': '#fecc5c',
'min': 45, 'label': 'Low', 'size': 1},
{'opacity': 1, 'max': 350, 'colour': '#fd8d3c',
'min': 201, 'label': 'Medium', 'size': 2},
{'opacity': 1, 'max': 539, 'colour': '#f31a1c',
'min': 351, 'label': 'High', 'size': 3}]}
print 'Setting style with point sizes should work.'
set_vector_graduated_style(layer, style_info)
# Now validate the size values were set as expected
# new symbology - subclass of QgsFeatureRendererV2 class
renderer = layer.rendererV2()
layer_type = renderer.type()
assert layer_type == 'graduatedSymbol'
size = 1
for renderer_range in renderer.ranges():
symbol = renderer_range.symbol()
symbol_layer = symbol.symbolLayer(0)
actual_size = symbol_layer.size()
message = ((
'Expected symbol layer 0 for range %s to have'
' a size of %s, got %s') %
(size, size, actual_size))
assert size == actual_size, message
size += 1
示例12: test_renderComposition
def test_renderComposition(self):
"""Test making an image of the map only."""
LOGGER.info("Testing renderComposition")
myLayer, _ = load_layer("test_shakeimpact.shp")
myCanvasLayer = QgsMapCanvasLayer(myLayer)
CANVAS.setLayerSet([myCanvasLayer])
myRect = QgsRectangle(106.7894, -6.2308, 106.8004, -6.2264)
CANVAS.setExtent(myRect)
CANVAS.refresh()
myMap = Map(IFACE)
myMap.set_impact_layer(myLayer)
myMap.compose_map()
myImagePath, myControlImage, myTargetArea = myMap.render()
LOGGER.debug(myImagePath)
assert myControlImage is not None
myDimensions = [myTargetArea.left(), myTargetArea.top(), myTargetArea.bottom(), myTargetArea.right()]
myExpectedDimensions = [0.0, 0.0, 3507.0, 2480.0]
myMessage = "Expected target area to be %s, got %s" % (str(myExpectedDimensions), str(myDimensions))
assert myExpectedDimensions == myDimensions, myMessage
myMessage = "Rendered output does not exist"
assert os.path.exists(myImagePath), myMessage
# Beta version and version changes can introduce a few extra chars
# into the metadata section so we set a reasonable tolerance to cope
# with this.
myTolerance = 8000
myFlag, myMessage = check_images("renderComposition", myImagePath, myTolerance)
assert myFlag, myMessage
示例13: test_add_class_to_legend
def test_add_class_to_legend(self):
"""Test we can add a class to the map legend."""
layer, _ = load_layer('test_shakeimpact.shp')
map_legend = MapLegend(layer)
colour = QtGui.QColor(12, 34, 126)
map_legend.add_class(
colour,
label='bar')
map_legend.add_class(
colour,
label='foo')
path = unique_filename(
prefix='addClassToLegend',
suffix='.png',
dir=temp_dir('test'))
map_legend.get_legend().save(path, 'PNG')
LOGGER.debug(path)
# As we have discovered, different versions of Qt and
# OS platforms cause different output, so place any other possible
# variants in the safe_qgis/test_data/test_images/ dir e.g.
# addClassToLegend-variantUbuntu13.04.png
tolerance = 0 # to allow for version number changes in disclaimer
flag, message = check_images(
'addClassToLegend', path, tolerance)
message += (
'\nWe want these images to match, if they do already copy the test'
' image generated to create a new control image.')
self.assertTrue(flag, message)
示例14: test_printImpactTable
def test_printImpactTable(self):
"""Test that we can render html from impact table keywords."""
LOGGER.debug('InaSAFE HtmlRenderer testing printImpactTable')
myFilename = 'test_floodimpact.tif'
myLayer, _ = load_layer(myFilename)
myMessage = 'Layer is not valid: %s' % myFilename
assert myLayer.isValid(), myMessage
myPageDpi = 300
myHtmlRenderer = HtmlRenderer(myPageDpi)
myPath = unique_filename(prefix='impactTable',
suffix='.pdf',
dir=temp_dir('test'))
myKeywordIO = KeywordIO()
myKeywords = myKeywordIO.read_keywords(myLayer)
myPath = myHtmlRenderer.print_impact_table(myKeywords,
filename=myPath)
myMessage = 'Rendered output does not exist: %s' % myPath
assert os.path.exists(myPath), myMessage
# pdf rendering is non deterministic so we can't do a hash check
# test_renderComposition renders just the image instead of pdf
# so we hash check there and here we just do a basic minimum file
# size check.
mySize = os.stat(myPath).st_size
myExpectedSizes = [20936, # as rendered on linux ub 12.04 64
21523, # as rendered on linux ub 12.10 64
20605, # as rendered on linux ub 13.04 64
21527, # as rendered on Jenkins post 22 June 2013
377191, # as rendered on OSX
252699L, # as rendered on Windows 7 64 bit
251782L, # as rendered on Windows 8 64 bit amd
21491, # as rendered on Slackware64 14.0
]
print 'Output pdf to %s' % myPath
self.assertIn(mySize, myExpectedSizes)
示例15: test_get_vector_legend
def test_get_vector_legend(self):
"""Getting a legend for a vector layer works.
.. note:: This test is not related do thousand separator since we
insert our own legend notes and our own layer.
"""
layer, _ = load_layer('test_shakeimpact.shp')
map_legend = MapLegend(
layer,
legend_notes='Thousand separator represented by \',\'',
legend_units='(people per cell)')
image = map_legend.vector_legend()
path = unique_filename(
prefix='getVectorLegend',
suffix='.png',
dir=temp_dir('test'))
image.save(path, 'PNG')
LOGGER.debug(path)
# As we have discovered, different versions of Qt and
# OS platforms cause different output, so myControlImages is a list
# of 'known good' renders.
tolerance = 0 # to allow for version number changes in disclaimer
flag, message = check_images(
'getVectorLegend', path, tolerance)
message += (
'\nWe want these images to match, if they do already '
'copy the test image generated to create a new control image.')
self.assertTrue(flag, message)