Navigation Menu

Skip to content

Commit

Permalink
Fix pylint and pep8 errors (all resolved now).
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed May 2, 2014
1 parent 6498c4a commit 6616f47
Show file tree
Hide file tree
Showing 15 changed files with 490 additions and 116 deletions.
2 changes: 1 addition & 1 deletion pylintrc
Expand Up @@ -35,7 +35,7 @@ profile=no

# Add files or directories to the blacklist. They should be base names, not
# paths.
ignore=odict.py,help_base.py,keywords_dialog_base.py,wizard_dialog_base.py,options_dialog_base.py,dock_base.py,resources.py,resources_rc.py,function_browser_base.py,function_options_dialog_base.py,minimum_needs_base.py,osm_downloader_base.py,shakemap_importer_base.py,osm_downloader_base.py,batch_dialog_base.py,impact_merge_dialog_base.py,impact_report_dialog_base.py
ignore=odict.py,help_base.py,keywords_dialog_base.py,wizard_dialog_base.py,options_dialog_base.py,dock_base.py,resources.py,resources_rc.py,function_browser_base.py,function_options_dialog_base.py,minimum_needs_base.py,osm_downloader_base.py,shakemap_importer_base.py,osm_downloader_base.py,batch_dialog_base.py,impact_merge_dialog_base.py,impact_report_dialog_base.py,about_dialog_base.py

# Pickle collected data for later comparisons.
persistent=yes
Expand Down
27 changes: 27 additions & 0 deletions run-linux-pycharm.sh
@@ -0,0 +1,27 @@
#!/bin/bash

QGIS_PREFIX_PATH=/usr/local/qgis-2.0
if [ -n "$1" ]; then
QGIS_PREFIX_PATH=$1
fi

echo $QGIS_PREFIX_PATH


export QGIS_PREFIX_PATH=$QGIS_PREFIX_PATH
export QGIS_PATH=$QGIS_PREFIX_PATH
export LD_LIBRARY_PATH=${QGIS_PREFIX_PATH}/lib
export PYTHONPATH=${QGIS_PREFIX_PATH}/share/qgis/python:${QGIS_PREFIX_PATH}/share/qgis/python/plugins:${PYTHONPATH}

echo "QGIS PATH: $QGIS_PREFIX_PATH"
export QGIS_DEBUG=0
export QGIS_LOG_FILE=/tmp/inasafe/realtime/logs/qgis.log
export QGIS_DEBUG_FILE=/tmp/inasafe/realtime/logs/qgis-debug.log

export PATH=${QGIS_PREFIX_PATH}/bin:$PATH

export INASAFE_WORK_DIR=/tmp/quake
export INASAFE_POPULATION_PATH=`pwd`/realtime/fixtures/exposure/population.tif
export INASAFE_LOCALE=id

/usr/local/bin/qgis-pycharm
11 changes: 5 additions & 6 deletions safe/common/gdal_ogr_tools.py
Expand Up @@ -14,7 +14,6 @@
import os

import numpy
import math
from osgeo import gdal, ogr, osr

from safe.common.utilities import unique_filename
Expand All @@ -23,15 +22,15 @@


def polygonize_thresholds(
raster_file_name,
threshold_min=0.0,
threshold_max=float('inf')):
raster_file_name,
threshold_min=0.0,
threshold_max=float('inf')):
"""
Function to polygonize raster. Areas (pixels) with threshold_min <
pixel_values < threshold_max will be converted to polygons.
:param raster: Raster file name
:type raster: string
:param raster_file_name: Raster file name
:type raster_file_name: string
:param threshold_min: Value that splits raster to
flooded or not flooded.
Expand Down
2 changes: 2 additions & 0 deletions safe/common/qgis_raster_tools.py
Expand Up @@ -12,6 +12,7 @@
from safe.common.utilities import unique_filename
from safe.storage.raster import qgis_imported
from safe.common.gdal_ogr_tools import polygonize_thresholds
from safe.common.exceptions import GetDataError

if qgis_imported: # Import QgsRasterLayer if qgis is available
# noinspection PyPackageRequirements
Expand Down Expand Up @@ -54,6 +55,7 @@ def _get_pixel_coordinates(extent, width, height, row, col):
:returns: output_x,output_y Output coordinates
:rtype: (double, double)
"""
# pylint: disable=
x_minimum = extent.xMinimum()
y_maximum = extent.yMaximum()
x_resolution = (extent.xMaximum() - extent.xMinimum()) / width
Expand Down
47 changes: 30 additions & 17 deletions safe/common/qgis_vector_tools.py
@@ -1,4 +1,4 @@

# coding=utf-8
"""**Utilities around QgsVectorLayer**
"""

Expand All @@ -24,7 +24,6 @@
QgsPoint,
QgsGeometry,
QgsFeatureRequest,
QgsRectangle,
QgsVectorFileWriter
)

Expand Down Expand Up @@ -62,6 +61,7 @@ def points_to_rectangles(points, dx, dy):
attrs = feature.attributes()
point = feature.geometry().asPoint()
x, y = point.x(), point.y()
# noinspection PyCallByClass,PyTypeChecker
g = QgsGeometry.fromPolygon([
[QgsPoint(x, y),
QgsPoint(x + dx, y),
Expand Down Expand Up @@ -118,7 +118,7 @@ def create_layer(vector):
:param vector: Vector layer
:type vector: QgsVectorLayer
:returns: Emply vector layer (stored in memory)
:returns: Empty vector layer (stored in memory)
:rtype: QgsVectorLayer
"""
crs = vector.crs().toWkt()
Expand Down Expand Up @@ -216,13 +216,13 @@ def split_by_polygon(
:rtype: QgsVectorLayer
"""

def _set_feature(geometry, attributes):
def _set_feature(geometry, feature_attributes):
"""
Helper to create and set up feature
"""
included_feature = QgsFeature()
included_feature.setGeometry(geometry)
included_feature.setAttributes(attributes)
included_feature.setAttributes(feature_attributes)
return included_feature

def _update_attr_list(attributes, index, value, add_attribute=False):
Expand Down Expand Up @@ -324,20 +324,31 @@ def _update_attr_list(attributes, index, value, add_attribute=False):


def split_by_polygon_in_out(
vector,
polygon_in,
polygon_out,
target_field, value,
request=QgsFeatureRequest()
):
vector,
polygon_in,
polygon_out,
target_field,
value,
request=QgsFeatureRequest()):

"""
:param vector:
:param polygon_in:
:param polygon_out:
:param target_field:
:param value:
:param request:
:return:
"""
base_name = unique_filename()
file_name_in = base_name + '_in.shp'
file_name_out = base_name + '_out.shp'

file_name_poly_in = base_name + '_poly_in.shp'
file_name_poly_out = base_name + '_poly_out.shp'

# noinspection PyArgumentEqualDefault
line_layer_in = split_by_polygon2(
vector,
polygon_in,
Expand Down Expand Up @@ -388,12 +399,15 @@ def split_by_polygon2(
:param vector: Vector layer
:type vector: QgsVectorLayer
:param polygon: Splitting polygons layer
:type polygon: QgsVectorLayer
:param polygon_layer: Splitting polygons layer
:type polygon_layer: QgsVectorLayer
:param request: Filter for vector objects
:type request: QgsFeatureRequest
:param use_contains_operation: Whether to use geometrical containment.
:type use_contains_operation: bool
:param mark_value: Field value to mark the objects.
:type mark_value: (field_name, field_value).or None
Expand Down Expand Up @@ -459,7 +473,6 @@ def _update_attr_list(attributes, index, value, add_attribute=False):
geometry_type = initial_geom.type()

poly_geoms = []
poly_bboxs = []
for polygon_feature in polygon_layer.getFeatures(request):
polygon = polygon_feature.geometry()
poly_geoms.append(QgsGeometry(polygon))
Expand Down Expand Up @@ -490,7 +503,7 @@ def _update_attr_list(attributes, index, value, add_attribute=False):
target_field_index,
target_value,
add_attribute=new_field_added
)
)
else:
new_attributes = attributes
feature = _set_feature(g, new_attributes)
Expand All @@ -499,7 +512,7 @@ def _update_attr_list(attributes, index, value, add_attribute=False):
else:
intersection = QgsGeometry(
initial_geom.intersection(polygon)
).asGeometryCollection()
).asGeometryCollection()

for g in intersection:
if g.type() == geometry_type:
Expand All @@ -509,7 +522,7 @@ def _update_attr_list(attributes, index, value, add_attribute=False):
target_field_index,
target_value,
add_attribute=new_field_added
)
)
else:
new_attributes = attributes
feature = _set_feature(g, new_attributes)
Expand Down
35 changes: 17 additions & 18 deletions safe/common/test_gdal_ogr_tools.py
Expand Up @@ -17,14 +17,12 @@
__copyright__ = ('Copyright 2012, Australia Indonesia Facility for '
'Disaster Reduction')


import os
import unittest
from osgeo import ogr
from safe.common.testing import UNITDATA

from safe.common.gdal_ogr_tools import (
polygonize_thresholds)
from safe.common.testing import UNITDATA
from safe.common.gdal_ogr_tools import polygonize_thresholds


class TestGDALOGRTools(unittest.TestCase):
Expand All @@ -38,26 +36,27 @@ def test_polygonize_thresholds(self):
'hazard',
'jakarta_flood_design.tif')

(
inside_file_name,
inside_layer_name,
outside_file_name,
outside_layer_name
) = polygonize_thresholds(raster_name, 0.5)
inside_file_name, inside_layer_name, outside_file_name, \
outside_layer_name = polygonize_thresholds(
raster_name, 0.5)

# Syntactic sugar to ignore unused vars.
_ = inside_layer_name
_ = outside_layer_name

driver = ogr.GetDriverByName('ESRI Shapefile')

dataSource = driver.Open(inside_file_name, 0)
layer = dataSource.GetLayer()
featureCount = layer.GetFeatureCount()
data_source = driver.Open(inside_file_name, 0)
layer = data_source.GetLayer()
feature_count = layer.GetFeatureCount()
#print 'inside %s' % (inside_file_name)
self.assertEquals(featureCount, 3)
self.assertEquals(feature_count, 3)

dataSource2 = driver.Open(outside_file_name, 0)
layer2 = dataSource2.GetLayer()
featureCount2 = layer2.GetFeatureCount()
data_source2 = driver.Open(outside_file_name, 0)
layer2 = data_source2.GetLayer()
feature_count2 = layer2.GetFeatureCount()
#print 'outside %s' % (outside_file_name)
self.assertEquals(featureCount2, 1)
self.assertEquals(feature_count2, 1)


if __name__ == '__main__':
Expand Down
46 changes: 19 additions & 27 deletions safe/common/test_qgis_vector_tools.py
Expand Up @@ -20,12 +20,11 @@
import os
import unittest

from safe.common.utilities import unique_filename
from safe.common.testing import UNITDATA, get_qgis_app
from safe.storage.raster import qgis_imported
from safe.common.gdal_ogr_tools import (
polygonize_thresholds)
if qgis_imported: # Import QgsRasterLayer if qgis is available
if qgis_imported: # Import QgsRasterLayer if qgis is available
QGIS_APP, CANVAS, IFACE, PARENT = get_qgis_app()
from PyQt4.QtCore import QVariant
from qgis.core import (
Expand All @@ -34,8 +33,7 @@
QgsField,
QgsFeature,
QgsGeometry,
QgsRectangle,
QgsVectorFileWriter)
QgsRectangle)


from safe.common.qgis_vector_tools import (
Expand Down Expand Up @@ -203,14 +201,14 @@ def test_split_by_polygon(self):

# Split by the extent (The result is the copy of the layer)
line_before.updateExtents()
# Expand extent to cover the lines (add EPSILON to bounds)
EPSILON = 0.0001 # A small number
# Expand extent to cover the lines (add epsilon to bounds)
epsilon = 0.0001 # A small number
extent = line_before.extent()
new_extent = QgsRectangle(
extent.xMinimum() - EPSILON,
extent.yMinimum() - EPSILON,
extent.xMaximum() + EPSILON,
extent.yMaximum() + EPSILON
extent.xMinimum() - epsilon,
extent.yMinimum() - epsilon,
extent.xMaximum() + epsilon,
extent.yMaximum() + epsilon
)
new_extent = QgsGeometry().fromRect(new_extent)
split_lines = split_by_polygon(
Expand Down Expand Up @@ -269,32 +267,26 @@ def test_split_by_polygon_in_out(self):
'EXPOSURE',
'ogr')

(
inside_file_name,
inside_layer_name,
outside_file_name,
outside_layer_name
) = polygonize_thresholds(raster_name, 0.1)
inside_file_name, inside_layer_name, outside_file_name, \
outside_layer_name = polygonize_thresholds(raster_name, 0.1)

polygon_in = \
QgsVectorLayer(inside_file_name, inside_layer_name, 'ogr')
polygon_out = \
QgsVectorLayer(outside_file_name, outside_layer_name, 'ogr')

layer = split_by_polygon_in_out(qgis_exposure,
polygon_in,
polygon_out,
'flooded', 1)
layer = split_by_polygon_in_out(
qgis_exposure, polygon_in, polygon_out, 'flooded', 1)

featureCount = layer.featureCount()
self.assertEqual(featureCount, 184)
feature_count = layer.featureCount()
self.assertEqual(feature_count, 184)

flooded = 0
iter = layer.getFeatures()
for feature in iter:
attrs = feature.attributes()
if (attrs[3] == 1):
flooded = flooded + 1
iterator = layer.getFeatures()
for feature in iterator:
attributes = feature.attributes()
if attributes[3] == 1:
flooded += 1
self.assertEqual(flooded, 25)


Expand Down

0 comments on commit 6616f47

Please sign in to comment.