vector.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. ***************************************************************************
  3. vector.py
  4. ---------------------
  5. Date : February 2013
  6. Copyright : (C) 2013 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'February 2013'
  19. __copyright__ = '(C) 2013, Victor Olaya'
  20. from qgis.core import (NULL,
  21. QgsFeatureRequest)
  22. def resolveFieldIndex(source, attr):
  23. """This method takes an object and returns the index field it
  24. refers to in a layer. If the passed object is an integer, it
  25. returns the same integer value. If the passed value is not an
  26. integer, it returns the field whose name is the string
  27. representation of the passed object.
  28. Ir raises an exception if the int value is larger than the number
  29. of fields, or if the passed object does not correspond to any
  30. field.
  31. """
  32. if isinstance(attr, int):
  33. return attr
  34. else:
  35. index = source.fields().lookupField(attr)
  36. if index == -1:
  37. raise ValueError('Wrong field name')
  38. return index
  39. def values(source, *attributes):
  40. """Returns the values in the attributes table of a feature source,
  41. for the passed fields.
  42. Field can be passed as field names or as zero-based field indices.
  43. Returns a dict of lists, with the passed field identifiers as keys.
  44. It considers the existing selection.
  45. It assumes fields are numeric or contain values that can be parsed
  46. to a number.
  47. """
  48. ret = {}
  49. indices = []
  50. attr_keys = {}
  51. for attr in attributes:
  52. index = resolveFieldIndex(source, attr)
  53. indices.append(index)
  54. attr_keys[index] = attr
  55. # use an optimised feature request
  56. request = QgsFeatureRequest().setSubsetOfAttributes(indices).setFlags(QgsFeatureRequest.NoGeometry)
  57. for feature in source.getFeatures(request):
  58. for i in indices:
  59. # convert attribute value to number
  60. try:
  61. v = float(feature[i])
  62. except:
  63. v = None
  64. k = attr_keys[i]
  65. if k in ret:
  66. ret[k].append(v)
  67. else:
  68. ret[k] = [v]
  69. return ret
  70. def convert_nulls(values, replacement=None):
  71. """
  72. Converts NULL items in a list of values to a replacement value (usually None)
  73. :param values: list of values
  74. :param replacement: value to use in place of NULL
  75. :return: converted list
  76. """
  77. return [i if i != NULL else replacement for i in values]
  78. def checkMinDistance(point, index, distance, points):
  79. """Check if distance from given point to all other points is greater
  80. than given value.
  81. """
  82. if distance == 0:
  83. return True
  84. neighbors = index.nearestNeighbor(point, 1)
  85. if len(neighbors) == 0:
  86. return True
  87. if neighbors[0] in points:
  88. np = points[neighbors[0]]
  89. if np.sqrDist(point) < (distance * distance):
  90. return False
  91. return True