Example #1
0
def computeGeoDist(G):
	"""Compute Geographical distances with waypoints"""
	path = nx.all_pairs_dijkstra_path(G)
	outf = open("african-origin-homelands-distances-1.txt", "w")
	for src, src_coordl in source_dict.iteritems():
#		print src, src_coordl
		src_area = src_coordl[0]
		src_coord = src_coordl[1]
		for hm, hm_coordl in hmcoord_dict.iteritems():
			hm_area = hm_coordl[0]
			hm_coord = hm_coordl[1]
			distance = 0
			if src_area != hm_area:
				path_list = path[src_area][hm_area]
				coord_list = []
				coord_list.append(src_coord)
#				distance += getDistance(src_coord,G[src_area][path_list[1]]["coord"])
				for i in range(len(path_list)-1):
					coord_list.append(G[path_list[i]][path_list[i+1]]["coord"])
				coord_list.append(hm_coord)
#				print coord_list				
				for i in range(len(coord_list)-1):
					distance += getDistance(coord_list[i],coord_list[i+1])
			else:
				distance = getDistance(src_coord, hm_coord)
			outf.write(src+"\t"+hm+"\t"+str(distance)+"\n")
			print src, hm, distance
Example #2
0
def getclosestpoint (curpos):
    for pos in range(len(latlngdata)-1):
        pathpos =  (latlngdata[pos][0], latlngdata[pos][1])
        dist = getDistance(pathpos,curpos)
        pathpos =  (latlngdata[pos+1][0], latlngdata[pos+1][1])
        nextdist = getDistance(pathpos,curpos)
        if nextdist >= dist:
            break
        #~ print dist
        #~ print pos
    return sum(distancedata[0:pos])
def getclosestpoint (curpos,lastpos):
	for pos in range(len(latlngdata)-1):
		#~ savedpos = pos # iterate only over future positions
		#~ pos = savedpos + pos # iterate only over future positions
		pathpos =  (latlngdata[pos+lastpos][0], latlngdata[pos+lastpos][1])
		dist = getDistance(pathpos,curpos)
		pathpos =  (latlngdata[pos+lastpos+1][0], latlngdata[pos+lastpos+1][1])
		nextdist = getDistance(pathpos,curpos)
		if nextdist >= dist:
			break
		#~ print dist
		#~ print pos
		#~ savedpos = pos # iterate only over future positions
	return (sum(distancedata[0:pos+lastpos]),pos+lastpos)
def nearestTubeStations(geolocation):
	r = []
	for uuid in tube_stations:
		geo_station=tube_stations[uuid][4]
		if (gislib.isWithinDistance(geolocation,geo_station,0.9)):
			distance_km = gislib.getDistance(geolocation,geo_station)
			r.append((tube_stations[uuid][2],int(1000*distance_km),calculate_walking_time(distance_km)))
	return r
  def __findNearest_helper(self, db, lat, lon, bearing, speed):
    cur = db.cursor()

    # SQL to define a circle around the home point with a given radius

    circle = 'BuildCircleMbr(%f, %f, %f)' % (lat, lon, self.__radius / 111319.0)

    # SQL to calculate distance between geocache and home point
    dist = 'greatcirclelength(geomfromtext("linestring(" || x(location) || " " || y(location) || ", %f %f)", 4326))' % (lat, lon)

    # Query for caches within circle and order by distance
    rs = cur.execute('select code, description, x(location), y(location), URL from gc where MbrWithin(location, %(searchCircle)s)' % {'dist':dist, 'searchCircle':circle})

    data = []
    for row in rs:
      coord = (row[2], row[3])
      data.append({
        'code': row[0],
        'description': row[1],
        'URL': row[4],
        'distance': int(1000.0 * gislib.getDistance(coord, (lat, lon))),
        'bearing': gislib.calculateBearing((lat, lon), coord),
        'position': coord
        })

    if len(data) == 0: return None

    # sort by distance again since we aren't using the database distance
    # this should be removed if I switch back to using the Spatialite dist.
    data.sort(key=lambda x: x['distance'])

    # If first cache is within closeRadius return that 
    if data[0]['distance'] < CLOSE_RADIUS:
      print "cache within ", CLOSE_RADIUS, "m"
      return data[0] 

    # only right in front of us.
    if (speed < .2778 * SPEED_THRESHOLD): 
      print "slow speed", (speed / .2778), "km/h"
      return data[0]

    # sift through caches looking for closest one within our travel path
    for row in data:
      # look for caches within 200m of path of travel
      distanceFromPath = geom.distanceFromPath(bearing, row['bearing'], row['distance'])
      if distanceFromPath and distanceFromPath < MAXIMUM_DISTANCE_FROM_PATH:
        return row

      # look for cache within 30 degree arc.  This has dumb behaviour if dist is large
      #if gislib.isAngleWithin(bearing, row['bearing'], 15):
      #  return row

    return None
Example #6
0
    def __findNearest_helper(self, db, lat, lon, bearing, speed):
        cur = db.cursor()

        # SQL to define a circle around the home point with a given radius
        circle = 'BuildCircleMbr(%f, %f, %f)' % (lat, lon,
                                                 self.__radius / 111319.0)

        # SQL to calculate distance between geocache and home point
        dist = 'greatcirclelength(geomfromtext("linestring(" || x(location) || " " || y(location) || ", %f %f)", 4326))' % (
            lat, lon)

        # Query for caches within circle and order by distance
        rs = cur.execute(
            'select code, description, x(location), y(location) from gc where MbrWithin(location, %(searchCircle)s)'
            % {
                'dist': dist,
                'searchCircle': circle
            })

        data = []
        for row in rs:
            coord = (row[2], row[3])
            data.append({
                'code':
                row[0],
                'description':
                row[1],
                'distance':
                int(1000.0 * gislib.getDistance(coord, (lat, lon))),
                'bearing':
                gislib.calculateBearing((lat, lon), coord),
                'position':
                coord
            })

        if len(data) == 0: return None

        # sort by distance again since we aren't using the database distance
        # this should be removed if I switch back to using the Spatialite dist.
        data.sort(key=lambda x: x['distance'])

        # If first cache is within closeRadius return that
        if data[0]['distance'] < CLOSE_RADIUS:
            print "cache within ", CLOSE_RADIUS, "m"
            return data[0]

        # only right in front of us.
        if (speed < .2778 * SPEED_THRESHOLD):
            print "slow speed", (speed / .2778), "km/h"
            return data[0]

        # sift through caches looking for closest one within our travel path
        for row in data:
            # look for caches within 200m of path of travel
            distanceFromPath = geom.distanceFromPath(bearing, row['bearing'],
                                                     row['distance'])
            if distanceFromPath and distanceFromPath < MAXIMUM_DISTANCE_FROM_PATH:
                return row

            # look for cache within 30 degree arc.  This has dumb behaviour if dist is large
            #if gislib.isAngleWithin(bearing, row['bearing'], 15):
            #  return row

        return None