def test_simple_accurate(self, precision=3):
        glen = lambda x: len(list(x))
        index = GeoGridIndex(precision=precision)
        map(index.add_point, self.points)

        ls = index.get_nearest_points(self.point_1bluxome, 10)
        ls = list(ls)
        eq_(glen(ls), 2)
        points = map(itemgetter(0), ls)
        self.assertIn(self.point_1bluxome, points)
        self.assertIn(self.point_market_street, points)

        eq_(glen(index.get_nearest_points(self.point_1bluxome, 15)), 3)
        eq_(glen(index.get_nearest_points(self.point_1bluxome, 34)), 4)
    def test_simple_accurate(self, precision=3):
        glen = lambda x: len(list(x))
        index = GeoGridIndex(precision=precision)
        map(index.add_point, self.points)

        ls = index.get_nearest_points(self.point_1bluxome, 10)
        ls = list(ls)
        eq_(glen(ls), 2)
        points = map(itemgetter(0), ls)
        self.assertIn(self.point_1bluxome, points)
        self.assertIn(self.point_market_street, points)

        eq_(glen(index.get_nearest_points(self.point_1bluxome, 15)), 3)
        eq_(glen(index.get_nearest_points(self.point_1bluxome, 34)), 4)
Example #3
0
class QuietPlacesData:
    def __init__(self, datafile, debug):
        self.debug = debug
        fileObject = open(datafile, 'r')
        self.data = pickle.load(fileObject)
        # print 'loaded ' + str(len(self.data)) + 'records, initializing index'
        self.__load_geo_index()

    def __load_geo_index(self):
        self.geo_index = GeoGridIndex()
        for lodging in self.data:
            # if self.debug: print 'loading geo for:' + lodging["name"]
            lat = float(lodging["lat"])
            lng = float(lodging["lng"])
            self.geo_index.add_point(GeoPoint(lat, lng, ref=lodging))

    def geo_search(self, lat, lng, range):
        center_point = GeoPoint(lat, lng)
        lodgings = []

        for geo_point, distance in self.geo_index.get_nearest_points(
                center_point, range, 'km'):
            # if self.debug: print("We found {0} in {1} km".format(geo_point.ref["name"], distance))
            lodgings.append(geo_point.ref)
        return lodgings
 def test_bounds(self):
     glen = lambda x: len(list(x))
     # ezv block
     point1 = GeoPoint(43.59375, -4.21875)  # ezv
     point2 = GeoPoint(43.59375, -4.218750001)  # ezu
     point3 = GeoPoint(43.59375, -2.812500001)  # ezv
     point4 = GeoPoint(43.59375, -2.8125)  # ezy
     point5 = GeoPoint(43.59375, (-4.21875 + -2.8125) / 2)
     points = [point1, point2, point3, point4, point5]
     index = GeoGridIndex(precision=3)
     # import ipdb; ipdb.set_trace()
     map(index.add_point, points)
     self.assertEquals(glen(index.get_nearest_points(point1, 57)), 3)
     self.assertEquals(glen(index.get_nearest_points(point2, 57)), 3)
     self.assertEquals(glen(index.get_nearest_points(point3, 57)), 3)
     self.assertEquals(glen(index.get_nearest_points(point4, 57)), 3)
     self.assertEquals(glen(index.get_nearest_points(point5, 57)), 5)
def test_performance(count=10):
    index = GeoGridIndex()
    points = [get_random_point() for _ in range(count)]
    map(index.add_point, points)
    for point in points:
        ls = list(index.get_nearest_points(point, 20))
        assert len(ls) > 0
        assert point in map(itemgetter(0), ls)
 def test_wrong_precision(self):
     index = GeoGridIndex(precision=4)
     self.assertRaisesRegexp(
         Exception,
         'precision=2',
         lambda: list(
             index.get_nearest_points(self.point_market_street, 100))
     )
 def test_distance_km(self, precision=3):
     index = GeoGridIndex(precision=precision)
     map(index.add_point, self.points)
     for pt, distance in index.get_nearest_points(self.point_1bluxome, 10):
         if pt == self.point_1bluxome:
             self.assertEquals(distance, 0)
         if pt == self.point_market_street:
             self.assertEquals(distance, 1.301272755220718)
 def test_distance_km(self, precision=3):
     index = GeoGridIndex(precision=precision)
     map(index.add_point, self.points)
     for pt, distance in index.get_nearest_points(self.point_1bluxome, 10):
         if pt == self.point_1bluxome:
             self.assertEquals(distance, 0)
         if pt == self.point_market_street:
             self.assertEquals(distance, 1.301272755220718)
    def test_distance_mi(self, precision=3):
        index = GeoGridIndex(precision=precision)
        map(index.add_point, self.points)
        for pt, distance in index.get_nearest_points(
                self.point_1bluxome, 10, 'mi'):

            if pt == self.point_1bluxome:
                self.assertEquals(distance, 0)
            if pt == self.point_market_street:
                self.assertEquals(distance, 0.808573403337458)
    def test_distance_mi(self, precision=3):
        index = GeoGridIndex(precision=precision)
        map(index.add_point, self.points)
        for pt, distance in index.get_nearest_points(self.point_1bluxome, 10,
                                                     'mi'):

            if pt == self.point_1bluxome:
                self.assertEquals(distance, 0)
            if pt == self.point_market_street:
                self.assertEquals(distance, 0.808573403337458)
 def test_bounds(self):
     glen = lambda x: len(list(x))
     # ezv block
     point1 = GeoPoint(43.59375, -4.21875)  # ezv
     point2 = GeoPoint(43.59375, -4.218750001)  # ezu
     point3 = GeoPoint(43.59375, -2.812500001)  # ezv
     point4 = GeoPoint(43.59375, -2.8125)  # ezy
     point5 = GeoPoint(43.59375, (-4.21875 + -2.8125)/2)
     points = [point1, point2, point3, point4, point5]
     index = GeoGridIndex(precision=3)
     # import ipdb; ipdb.set_trace()
     map(index.add_point, points)
     self.assertEquals(glen(index.get_nearest_points(point1, 57)),
                       3)
     self.assertEquals(glen(index.get_nearest_points(point2, 57)),
                       3)
     self.assertEquals(glen(index.get_nearest_points(point3, 57)),
                       3)
     self.assertEquals(glen(index.get_nearest_points(point4, 57)),
                       3)
     self.assertEquals(glen(index.get_nearest_points(point5, 57)),
                       5)
Example #12
0
def match_stops_to_nodes(gtfs, walk_network):
    """
    Parameters
    ----------
    gtfs : a GTFS object
    walk_network : networkx.Graph

    Returns
    -------
    stop_I_to_node: dict
        maps stop_I to closest walk_network node
    stop_I_to_dist: dict
        maps stop_I to the distance to the closest walk_network node
    """
    network_nodes = walk_network.nodes(data="true")

    stop_Is = set(gtfs.get_straight_line_transfer_distances()['from_stop_I'])
    stops_df = gtfs.stops()

    geo_index = GeoGridIndex(precision=6)
    for net_node, data in network_nodes:
        geo_index.add_point(GeoPoint(data['lat'], data['lon'], ref=net_node))
    stop_I_to_node = {}
    stop_I_to_dist = {}
    for stop_I in stop_Is:
        stop_lat = float(stops_df[stops_df.stop_I == stop_I].lat)
        stop_lon = float(stops_df[stops_df.stop_I == stop_I].lon)
        geo_point = GeoPoint(stop_lat, stop_lon)
        min_dist = float('inf')
        min_dist_node = None
        search_distances_m = [0.100, 0.500]
        for search_distance_m in search_distances_m:
            for point, distance in geo_index.get_nearest_points(
                    geo_point, search_distance_m, "km"):
                if distance < min_dist:
                    min_dist = distance * 1000
                    min_dist_node = point.ref
            if min_dist_node is not None:
                break
        if min_dist_node is None:
            warn("No OSM node found for stop: " +
                 str(stops_df[stops_df.stop_I == stop_I]))
        stop_I_to_node[stop_I] = min_dist_node
        stop_I_to_dist[stop_I] = min_dist
    return stop_I_to_node, stop_I_to_dist
 def test_big_distance(self):
     index = GeoGridIndex(precision=2)
     map(index.add_point, self.points)
     ls = list(index.get_nearest_points(self.point_la, 600))
     self.assertEquals(len(ls), len(self.points))
Example #14
0
class DistanceCalculator(object):
    def __init__(self):
        self.geo_index = GeoGridIndex(precision=3)
        self.conn = sqlite3.connect('rockstar_02.db',
                                    isolation_level="DEFERRED")
        self.conn.text_factory = str
        self.cursor = self.conn.cursor()
        self.debug = False

    def load_index(self, input=None):
        """
		Load all of the geolocated cemetery towers into memory,
		inside of our geo_index variable
		"""
        print 'Loading locations of interest into internal spatial index.'
        input_counter = 0
        sf = shapefile.Reader(
            '/Users/chrisholloway/Downloads/virginia-latest-free.shp/gis.osm.roads_free_1.shp'
        )
        shaperec = sf.shapeRecords()
        motorway_hash = []
        for rec in range(len(shaperec)):
            if 'secondary_link' in shaperec[rec].record[2]:
                lat = shaperec[rec].shape.points[0][1]
                lon = shaperec[rec].shape.points[0][0]
                self.geo_index.add_point(GeoPoint(lat, lon))
                input_counter += 1
        print 'Done loading index of secondary_links (added %s values)' % (
            input_counter)

    def enumerate_all_distances(self, admin1=None):
        """
		Walk the geohash5 centroids,
		calculate the distance to the nearest tower for each one,
		and write the distance value to the database.
		"""
        #Walk the geohash5 centroids,
        c = self.cursor
        c.execute(
            'SELECT geohash, centroid_lat, centroid_lon from boxes where admin1=?',
            (admin1, ))
        geohashes_plus_coords = []
        for row in c.fetchall():
            geo5_item, lat, lon = row
            geohashes_plus_coords.append([geo5_item, lat, lon])
            #print geo5_item
        #print 'Those are the geohashes'
        progress_counter = 0
        for geo5, lat, lon in geohashes_plus_coords:
            progress_counter += 1
            if progress_counter % 50 == 0:
                print 'Processed %s records.' % (progress_counter)
            if self.debug == True:
                print '--------'
                print 'geohash of interest:', geo5, lat, lon
            #calculate the distance to the nearest tower for each one,
            temp_geo_point = GeoPoint(lat, lon)
            values = self.geo_index.get_nearest_points(temp_geo_point, 50.0,
                                                       'km')
            #print values
            minimum_distance = MINIMUM_DISTANCE
            for value in values:
                the_point, the_distance = value
                if the_distance < minimum_distance:
                    minimum_distance = the_distance

            #and write the distance value to the database.
            c.execute(
                'UPDATE boxes set secondary_link_distance=? where geohash=?',
                (minimum_distance, geo5))
        self.conn.commit()
        print 'Finished updating distance from geo5 centroids to input data'
Example #15
0
        "<p><font color='red'>You need to login first to acced to this page</font></p>"
    )

if 'point' in form and 'sid' in form:
    if form['type'].value == 'polyline':
        o1, o2, d1, d2 = (form['point'].value).split(',')[:4]
        o = o1 + ',' + o2
        d = d1 + ',' + d2
        olati, olongi = o[7:].rstrip(')').split(',')
        dlati, dlongi = d[7:].rstrip(')').split(',')
        center_point = GeoPoint(float(olati), float(olongi))
        z = int(form['zoom'].value)
        distance1 = dzoom[z]
        banda = form['banda'].value
        prop = int(form['sid'].value)
        for npoint, distance in geo_index.get_nearest_points(
                center_point, 10, 'km'):
            if distance < distance1:
                distance1 = distance
                olati, olongi = str(npoint)[6:].rstrip(')').split(',')
        center_point = GeoPoint(float(dlati), float(dlongi))
        distance1 = dzoom[z]
        for npoint, distance in geo_index.get_nearest_points(
                center_point, 10, 'km'):
            if distance < distance1:
                distance1 = distance
                dlati, dlongi = str(npoint)[6:].rstrip(')').split(',')
        ogh = geohash.encode(float(olati), float(olongi), 15)
        dgh = geohash.encode(float(dlati), float(dlongi), 15)
        if string.find(banda, '2') > 0:
            data = (ogh, dgh, 'b24ghz', 'buena', prop)
            if not (data in datapoint):
Example #16
0
       [0.        , 1.41421356],
       [0.        , 1.        ],
       [0.        , 1.        ],
       [0.        , 1.41421356]])


from geoindex import GeoGridIndex, GeoPoint
import random
index = GeoGridIndex()

for _ in range(10000):
    lat = random.random()*180 - 90
    lng = random.random()*360 - 180
    index.add_point(GeoPoint(lat, lng))




center_point = GeoPoint(37.7772448, -122.3955118)
for distance, point in index.get_nearest_points(center_point, 10, 'km'):
    print("We found {0} in {1} km".format(point, distance))



#index = GeoGridIndex()
for airport in get_all_airports():
    index.add_point(GeoPoint(lat, lng, ref=airport))

center_point = GeoPoint(37.7772448, -122.3955118)
for distance, point in index.get_nearest_points(center_point, 10, 'km'):
    print("We airport {0} in {1} km".format(point.ref, distance))
 def test_wrong_precision(self):
     index = GeoGridIndex(precision=4)
     self.assertRaisesRegexp(
         Exception, 'precision=2', lambda: list(
             index.get_nearest_points(self.point_market_street, 100)))
 def test_big_distance(self):
     index = GeoGridIndex(precision=2)
     map(index.add_point, self.points)
     ls = list(index.get_nearest_points(self.point_la, 600))
     self.assertEquals(len(ls), len(self.points))
class DistanceCalculator(object):
    def __init__(self):
        self.geo_index = GeoGridIndex(precision=3)
        self.conn = sqlite3.connect('rockstar_02.db',
                                    isolation_level="DEFERRED")
        self.conn.text_factory = str
        self.cursor = self.conn.cursor()
        self.debug = False

    def load_index(self, input=None):
        """
		Load all of the geolocated cemetery towers into memory,
		inside of our geo_index variable
		"""
        print 'Loading locations of interest into internal spatial index.'
        input_counter = 0
        for line in open(input, 'rU'):
            line = line.strip()
            parts = line.split('\t')
            #print parts
            #if len(parts) < 20 or len(parts)<2:
            #	continue
            admin1, lat, lon, tag = parts[10], float(parts[4]), float(
                parts[5]), parts[7]
            if admin1 == 'VA' and tag == 'HSP':
                #print lat
                self.geo_index.add_point(GeoPoint(lat, lon))
                input_counter += 1
        print 'Done loading index of hospital (added %s values)' % (
            input_counter)

    def enumerate_all_distances(self, admin1=None):
        """
		Walk the geohash5 centroids,
		calculate the distance to the nearest tower for each one,
		and write the distance value to the database.
		"""
        #Walk the geohash5 centroids,
        c = self.cursor
        c.execute(
            'SELECT geohash, centroid_lat, centroid_lon from boxes where admin1=?',
            (admin1, ))
        geohashes_plus_coords = []
        for row in c.fetchall():
            geo5_item, lat, lon = row
            geohashes_plus_coords.append([geo5_item, lat, lon])
            #print geo5_item
        #print 'Those are the geohashes'
        progress_counter = 0
        for geo5, lat, lon in geohashes_plus_coords:
            progress_counter += 1
            if progress_counter % 50 == 0:
                print 'Processed %s records.' % (progress_counter)
            if self.debug == True:
                print '--------'
                print 'geohash of interest:', geo5, lat, lon
            #calculate the distance to the nearest tower for each one,
            temp_geo_point = GeoPoint(lat, lon)
            values = self.geo_index.get_nearest_points(temp_geo_point, 50.0,
                                                       'km')
            #print values
            minimum_distance = MINIMUM_DISTANCE
            for value in values:
                the_point, the_distance = value
                if the_distance < minimum_distance:
                    minimum_distance = the_distance

            #and write the distance value to the database.
            c.execute('UPDATE boxes set hospital_distance=? where geohash=?',
                      (minimum_distance, geo5))
        self.conn.commit()
        print 'Finished updating distance from geo5 centroids to input data'
Example #20
0
	print("<p><font color='red'>You need to login first to acced to this page</font></p>")


if 'point' in form and 'sid' in form:
	if form['type'].value == 'polyline':
		o1,o2,d1,d2 = (form['point'].value).split(',')[:4]
		o= o1 + ',' + o2
		d= d1 + ',' + d2
		olati,olongi = o[7:].rstrip(')').split(',')
		dlati,dlongi = d[7:].rstrip(')').split(',')
		center_point = GeoPoint(float(olati),float(olongi))
		z = int(form['zoom'].value)
		distance1 = dzoom[z]
		banda = form['banda'].value
		prop = int(form['sid'].value)
		for npoint, distance in geo_index.get_nearest_points(center_point, 10, 'km'):
			if distance < distance1:
				distance1 = distance
				olati,olongi = str(npoint)[6:].rstrip(')').split(',')
		center_point = GeoPoint(float(dlati),float(dlongi))
		distance1 = dzoom[z]
		for npoint, distance in geo_index.get_nearest_points(center_point, 10, 'km'):
			if distance < distance1:
				distance1 = distance
				dlati,dlongi = str(npoint)[6:].rstrip(')').split(',')
		ogh = geohash.encode(float(olati),float(olongi),15)
		dgh = geohash.encode(float(dlati),float(dlongi),15)
		if string.find(banda,'2') > 0 :
			data = (ogh,dgh, 'b24ghz', 'buena',prop)
			if not(data in datapoint):
				insert_stmt = (