def standard_deviation_distance(obj_list, point_attribute_name='point'): """ Accepts a geoqueryset, list of objects or list of dictionaries, expected to contain objects with Point properties, and returns a float with the standard deviation distance of the provided points. The standard deviation distance is the average variation in the distance of points from the mean center. Unlike a standard deviation ellipse, it does not have a direction. By default, the function expects the Point field on your model to be called 'point'. If the point field is called something else, change the kwarg 'point_attribute_name' to whatever your field might be called. h3. Example usage >> import calculate >> calculate.standard_deviation_distance(qs) 0.046301584704149731 h3. Dependencies * "django":http://www.djangoproject.com/ * "geodjango":http://www.geodjango.org/ h3. Documentation * "standard deviation distance":http://www.spatialanalysisonline.com/\ output/html/Directionalanalysisofpointdatasets.html """ # Figure out what type of objects we're dealing with if isinstance(obj_list[0], type({})): def getkey(obj, key): return obj.get(key) gettr = getkey else: gettr = getattr mean = calculate.mean_center( obj_list, point_attribute_name=point_attribute_name ) distances = [ gettr(p, point_attribute_name).distance(mean) for p in obj_list ] return calculate.standard_deviation(distances)
def standard_deviation_distance(obj_list, point_attribute_name='point'): """ Accepts a geoqueryset, list of objects or list of dictionaries, expected to contain objects with Point properties, and returns a float with the standard deviation distance of the provided points. The standard deviation distance is the average variation in the distance of points from the mean center. Unlike a standard deviation ellipse, it does not have a direction. By default, the function expects the Point field on your model to be called 'point'. If the point field is called something else, change the kwarg 'point_attribute_name' to whatever your field might be called. h3. Example usage >> import calculate >> calculate.standard_deviation_distance(qs) 0.046301584704149731 h3. Dependencies * "django":http://www.djangoproject.com/ * "geodjango":http://www.geodjango.org/ h3. Documentation * "standard deviation distance":http://www.spatialanalysisonline.com/\ output/html/Directionalanalysisofpointdatasets.html """ # Figure out what type of objects we're dealing with if isinstance(obj_list[0], type({})): def getkey(obj, key): return obj.get(key) gettr = getkey else: gettr = getattr mean = calculate.mean_center(obj_list, point_attribute_name=point_attribute_name) distances = [ gettr(p, point_attribute_name).distance(mean) for p in obj_list ] return calculate.standard_deviation(distances)
def standard_deviation_distance(geoqueryset, point_attribute_name='point'): """ Accepts a geoqueryset, expected to contain objects with Point properties, and returns a float with the standard deviation distance of the provided points. The standard deviation distance is the average variation in the distance of points from the mean center. Unlike a standard deviation ellipse, it does not have a direction. By default, the function expects the Point field on your model to be called 'point'. If the point field is called something else, change the kwarg 'point_attribute_name' to whatever your field might be called. h3. Example usage >> import calculate >> calculate.standard_deviation_distance(qs) 0.046301584704149731 h3. Dependencies * "django":http://www.djangoproject.com/ * "geodjango":http://www.geodjango.org/ * "numpy":http://numpy.scipy.org/ h3. Documentation * "standard deviation distance":http://www.spatialanalysisonline.com/output/html/Directionalanalysisofpointdatasets.html """ if not isinstance(geoqueryset, GeoQuerySet): raise TypeError( 'First parameter must be a Django GeoQuerySet. You submitted a %s object' % type(geoqueryset)) mean = calculate.mean_center(geoqueryset, point_attribute_name=point_attribute_name) distances = [ getattr(p, point_attribute_name).distance(mean) for p in geoqueryset ] return calculate.standard_deviation(distances)
def test_mean_center(self): dict_list = [ { 'name': 'The Los Angeles Times', 'point': Point(-118.245517015, 34.0525260849, srid=4326) }, { 'name': 'The Higgins Building', 'point': Point(-118.245015, 34.051007, srid=4326) }, { 'name': 'Los Angeles City Hall', 'point': Point(-118.2430171966, 34.0535749927, srid=4326) }, ] self.assertEqual(type(calculate.mean_center(dict_list)), Point) self.assertEqual( calculate.mean_center(dict_list).wkt, 'POINT (-118.2445164038666690 34.0523693591999930)' ) class DummyObj(): def __init__(self, **entries): self.__dict__.update(entries) obj_list = [DummyObj(**d) for d in dict_list] self.assertEqual(type(calculate.mean_center(obj_list)), Point) self.assertEqual( calculate.mean_center(obj_list).wkt, 'POINT (-118.2445164038666690 34.0523693591999930)' ) class FakePoint(models.Model): fake_id = models.IntegerField(primary_key=True) name = models.TextField() point = models.PointField(srid=4326) obj_list = [ FakePoint(fake_id=i + 1, **d) for i, d in enumerate(dict_list) ] self.assertEqual(type(calculate.mean_center(obj_list)), Point) self.assertEqual( calculate.mean_center(obj_list).wkt, 'POINT (-118.2445164038666690 34.0523693591999930)' )
def standard_deviation_distance(geoqueryset, point_attribute_name='point'): """ Accepts a geoqueryset, expected to contain objects with Point properties, and returns a float with the standard deviation distance of the provided points. The standard deviation distance is the average variation in the distance of points from the mean center. Unlike a standard deviation ellipse, it does not have a direction. By default, the function expects the Point field on your model to be called 'point'. If the point field is called something else, change the kwarg 'point_attribute_name' to whatever your field might be called. h3. Example usage >> import calculate >> calculate.standard_deviation_distance(qs) 0.046301584704149731 h3. Dependencies * "django":http://www.djangoproject.com/ * "geodjango":http://www.geodjango.org/ * "numpy":http://numpy.scipy.org/ h3. Documentation * "standard deviation distance":http://www.spatialanalysisonline.com/output/html/Directionalanalysisofpointdatasets.html """ if not isinstance(geoqueryset, GeoQuerySet): raise TypeError('First parameter must be a Django GeoQuerySet. You submitted a %s object' % type(geoqueryset)) mean = calculate.mean_center(geoqueryset, point_attribute_name=point_attribute_name) distances = [getattr(p, point_attribute_name).distance(mean) for p in geoqueryset] return calculate.standard_deviation(distances)
def test_mean_center(self): dict_list = [ { 'name': 'The Los Angeles Times', 'point': Point(-118.245517015, 34.0525260849, srid=4326) }, { 'name': 'The Higgins Building', 'point': Point(-118.245015, 34.051007, srid=4326) }, { 'name': 'Los Angeles City Hall', 'point': Point(-118.2430171966, 34.0535749927, srid=4326) }, ] self.assertEqual(type(calculate.mean_center(dict_list)), Point) self.assertEqual( calculate.mean_center(dict_list).wkt, 'POINT (-118.2445164038666690 34.0523693591999930)') class DummyObj(): def __init__(self, **entries): self.__dict__.update(entries) obj_list = [DummyObj(**d) for d in dict_list] self.assertEqual(type(calculate.mean_center(obj_list)), Point) self.assertEqual( calculate.mean_center(obj_list).wkt, 'POINT (-118.2445164038666690 34.0523693591999930)') class FakePoint(models.Model): fake_id = models.IntegerField(primary_key=True) name = models.TextField() point = models.PointField(srid=4326) obj_list = [ FakePoint(fake_id=i + 1, **d) for i, d in enumerate(dict_list) ] self.assertEqual(type(calculate.mean_center(obj_list)), Point) self.assertEqual( calculate.mean_center(obj_list).wkt, 'POINT (-118.2445164038666690 34.0523693591999930)')