def query_potholes_within_bounds( self, bounds: Tuple[float, float, float, float], limit: int, photo_url_generator: Callable[[UUID], str]) -> List[Pothole]: north_east = geohash2.encode(bounds[0], bounds[1]) south_west = geohash2.encode(bounds[2], bounds[3]) with self.connection.cursor() as cursor: cursor.execute( """ SELECT id, device_name, recorded_on, confidence, latitude, longitude FROM pothole WHERE geohash BETWEEN %(south_west)s AND %(north_east)s ORDER BY random() -- TODO: Find better solution. LIMIT %(limit)s """, { 'north_east': north_east, 'south_west': south_west, 'limit': limit, }) return list([ Pothole( id=p[0], device_name=p[1], timestamp=p[2], confidence=p[3], coordinates=(p[4], p[5]), photo_url=photo_url_generator(p[0]), ) for p in cursor.fetchall() ])
def mapper(self, _, line): trip_difference = 0 for row in csv.reader([line]): #storing coordinates pickup_lat = row[17] pickup_lon = row[18] dropoff_lat = row[20] dropoff_lon = row[21] trip_seconds = row[4] if trip_seconds == 'Trip Seconds': #Skips header final_geohash = None trip_seconds = 0 continue if not pickup_lat or not pickup_lon \ or not dropoff_lat or not dropoff_lon: #skips rows with empty cells final_geohash = None trip_seconds = 0 continue #Computing final geohash and difference from trip time to the average time_of_day = process_time(row) try: pickup_geohash = geohash2.encode(float(pickup_lat), float(pickup_lon), 7) dropoff_geohash = geohash2.encode(float(dropoff_lat), float(dropoff_lon), 7) final_geohash = pickup_geohash + "_" + dropoff_geohash + "_" + time_of_day trip_difference = abs( (float(trip_seconds) - float(self.d_avg[final_geohash]))) except: final_geohash = None trip_seconds = 0 yield row, (final_geohash, trip_difference)
def get_min_max(points): min_loc = defaultdict(int) max_loc = defaultdict(int) for p in points: min_loc[geohash.encode(float(p[1]), float(p[0]), precision=5)] += 1 max_loc[geohash.encode(float(p[1]), float(p[0]), precision=7)] += 1 return len(min_loc), len(max_loc)
def insert_pothole(self, id: UUID, device_name: str, timestamp: datetime, confidence: float, coordinates: Tuple[float, float]) -> None: with self.connection.cursor() as cursor: cursor.execute( """ INSERT INTO pothole ( id, device_name, created_on, recorded_on, confidence, latitude, longitude, geohash ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """, ( str(id), device_name, _utc_now(), timestamp, confidence, coordinates[0], coordinates[1], geohash2.encode(coordinates[0], coordinates[1]), )) self.connection.commit()
def oneroom(addr): # 1. 동이름으로 위도 경도 구하기 url = "https://apis.zigbang.com/search?q={}".format(addr) response = requests.get(url) lat, lng = response.json()["items"][0]["lat"], response.json( )["items"][0]["lng"] # 2. 위도 경도로 geohash 알아내기 geohash = geohash2.encode(lat, lng, precision=5) # 3. geohash로 매물 리스트 가져오기 url = "https://apis.zigbang.com/v2/items?\ deposit_gteq=0&domain=zigbang&geohash={}&rent_gteq=0&sales_type_in=전세|월세&service_type_eq=원룸".format( geohash) response = requests.get(url) items = response.json()["items"] ids = [item["item_id"] for item in items] # 4. id로 매물 정보 가져오기 url = "https://apis.zigbang.com/v2/items/list" params = { "domain": "zigbang", "withCoalition": "false", "item_ids": ids[:900], } response = requests.post(url, params) items = response.json()["items"] return [item for item in items if addr in item["address"]]
def post(self): parser = reqparse.DataJSONParser(filter_=hail_expect_post) hj = parser.get_data()[0] hj['status'] = 'received' hail = models.Hail(**hj) models.db.session.add(hail) models.db.session.commit() g.hail_log = models.HailLog('POST', hail, request.data) send_request_operator.apply_async(args=[ hail.id, hail.operateur.hail_endpoint(current_app.config['ENV']), str(hail.operateur.operator_header_name), str(hail.operateur.operator_api_key), hail.operateur.email ], queue='send_hail_' + current_app.config['NOW']) influx_db.write_point( current_app.config['INFLUXDB_TAXIS_DB'], "hails_created", { "added_by": current_user.email, "operator": hail.operateur.email, "zupc": hail.ads_insee, "geohash": Geohash.encode(hail.customer_lat, hail.customer_lon), }) result = marshal({"data": [hail]}, hail_model) result['data'][0]['taxi']['lon'] = hail.initial_taxi_lon result['data'][0]['taxi']['lat'] = hail.initial_taxi_lat return result, 201
def mqtt_on_message_sensor(client, userdata, msg): sensor_data = json.loads(msg.payload) required_fields = {'battery', 'temperature', 'air_quality', 'radio_power'} required_tags = {'id'} geohash = geohash2.encode(sensor_data['latitude'], sensor_data['longitude']) influx_datapoints = [] for field in required_fields: influx_datapoint = { 'measurement': field, 'tags': { **{tag: sensor_data[tag] for tag in required_tags}, 'geohash': geohash }, 'time': int(sensor_data['time']), 'fields': { 'value': sensor_data[field] } } influx_datapoints.append(influx_datapoint) influxdb_client.write_points(influx_datapoints, time_precision='s') if not sensor_data['id'] in topology_node_references.keys(): logging.error('%s not yet in topology!', sensor_data['id']) else: topology_graph.update_vertex({ '_id': topology_node_references[sensor_data['id']]['_id'], 'coordinate': [sensor_data['latitude'], sensor_data['longitude']], 'geohash': geohash, **{field: sensor_data[field] for field in required_fields} })
def encode(self, lat, lon, precision=10, binary=False): """Encodes a lat/lon pair with Geohash. Parameters ---------- lat : float The latitude. long : float The longitude. precision : int (default=10) The length of the encoded location in Base32. binary : bool (default=False) If `True`, computes the binary codification of the Base32 geohash. Returns ------- geohash : str or array-like The Base32 geohash or a binary array-like representation if `binary=True`. """ hashed = gh.encode(lat, lon, precision) if binary: return np.concatenate([self.base32toBin[x] for x in hashed]) return hashed
async def post_item(request, user): data = request.json try: assert 'latitude' in data assert 'longitude' in data assert 'type' in data assert data['type'] in ('text', 'image') assert 'content' in data except AssertionError: return json({"error": "Empty field"}) data['content'] = data['content'].strip() data['geohash'] = geohash2.encode(data['latitude'], data['longitude'], precision=7) if data['type'] == 'image' and not check_image_existed(data['content']): return json({"error": "Image not existed"}) data['user'] = user try: item = await Item.create(**data) return json({ "id": item.id, "type": item.type, "content": item.content }) except Exception: return json({"error": "Abnormal data"})
def lookup(): """Add EV charger lookup to to redis Automatically converts coordinates to geohash to be stored in database Args (GET): latitude (float): Latitude of lookup longitdue (float): Longitude of lookup geohash (str): Geohash of lookup Returns (JSON): message (200): if successfully added lookup to redis error (400): if coordinates and Geohash are missing """ latitude = request.args.get("latitude") longitude = request.args.get("longitude") geohash = request.args.get("geohash") # error out when no coordinates given if not ((latitude and longitude) or geohash): return jsonify({"error": "require coordinates or geohash"}), 400 # convert coordinates to geohash if not geohash: geohash = geohash2.encode(float(latitude), float(longitude)) # add lookup to redis current_time = int(datetime.datetime.utcnow().timestamp()) redis_client.set(f"lookup:{geohash}:{current_time}", 1, ex=(60 * 60 * 24)) # store return jsonify({"message": "success"}), 200
def _encode(lat: float, lon: float, precision: float = 15) -> str: """ Encodes latitude/longitude to geohash. Either to specified precision or to automatically evaluated precision. Parameters ---------- lat : float This represents latitude in degrees. lon : float This represents longitude in degrees. precision : float, optional Number of characters in resulting geohash, by default 15 Return ------ str Geohash of supplied latitude/longitude. Example ------- >>> from pymove.utils.geoutils import _encode >>> lat1, lon1 = -3.777736, -38.547792 >>> lat2, lon2 = -3.793388, -38.517722 >>> print(_encode(lat1,lon1), type(_encode(lat1,lon1))) 7pkddb6356fyzxq <class 'str'> >>> print(_encode(lat2,lon2), type(_encode(lat2,lon2))) 7pkd7t2mbj0z1v7 <class 'str'> """ return gh.encode(lat, lon, precision)
def store_geo_metric(self, ip, geo_info, log_data): geo_metrics = [] geohash = encode(geo_info.location.latitude, geo_info.location.longitude) geohash_fields = {'count': 1} geohash_tags = {} geohash_tags['geohash'] = geohash geohash_tags['ip'] = ip geohash_tags['host'] = self.hostname geohash_tags['country_code'] = geo_info.country.iso_code geohash_tags['country_name'] = geo_info.country.name geohash_tags['state'] = geo_info.subdivisions.most_specific.name geohash_tags[ 'state_code'] = geo_info.subdivisions.most_specific.iso_code geohash_tags['city'] = geo_info.city.name geohash_tags['postal_code'] = geo_info.postal.code geohash_tags['latitude'] = geo_info.location.latitude geohash_tags['longitude'] = geo_info.location.longitude geo_metrics = [{ 'tags': geohash_tags, 'fields': geohash_fields, 'measurement': self.geo_measurement, }] logging.debug(f'Geo metrics: {geo_metrics}') try: self.influxdb.write_points(geo_metrics) except (InfluxDBServerError, ConnectionError) as e: logging.error( f'Error writing data to InfluxDB! Check your database!\nError: {e}' )
def save_state_data(self, county_wise_filepath): """ Data is formatted in the following way self.location_data = { "geohashes" : { "geohash": "location_hash" }, "locations": { "location_hash": [state, county, country, geohash, fips] } } """ self.location_data = {"geohashes": {}, "locations": {}} # load the data self.csv_data = pd.read_csv(county_wise_filepath, delimiter=',', index_col=[0], parse_dates=[0]) for _, row in self.csv_data.iterrows(): # only store unique data if self.location_data['locations'].get( row['Combined_Key']) is None: location_hash = row['Combined_Key'] state = row['Province_State'] county = row['Admin2'] country = row['Country_Region'] fips = row['FIPS'] county_geohash = '' try: lat = '' try: lat = float(row['Lat'].strip()) except AttributeError: lat = float(row['Lat']) log = '' try: log = float(row['Long_'].strip()) except AttributeError: log = float(row['Long_']) county_geohash = geohash2.encode( lat, log) # Generate Geohash for use with Grafana Plugin except Exception as e: print("Error Getting Geohash: ", e) # traceback.print_exc() if county_geohash != '': self.location_data['geohashes'][ county_geohash] = location_hash else: print("Empty Geohash! (", location_hash, ")") self.location_data['locations'][location_hash] = [ state, county, country, county_geohash, fips ]
def get_geohash(self, pos: tuple, level): """ :param pos: tuple(lat, lon) :param level: int :return: str """ return geohash.encode(pos[0], pos[1], precision=level)
def geocode(location_text, locator=default_locator): location = locator.geocode(location_text, timeout=10) if location: return geohash2.encode(location.latitude, location.longitude, precision=7) else: return None
def purchases(): idToken = request.args['idToken'] accountType = request.args['accountType'] # #check if token in valid # try: # decodedToken = auth.verify_id_token(idToken) # except: # return "INVALID USER TOKEN" # uid = decodedToken['uid'] # #determine accountId of the correct account accountId = getAccountId(accountType, customerId) if accountId == "": return "ACCOUNT TYPE DOES NOT EXIST" #retrieve all purchases url = 'http://api.reimaginebanking.com/accounts/{}/purchases?key={}'.format(accountId, apiKey) r = requests.get(url = url) # extracting data in json format purchaseData = r.json() locationFrequency = {} decodedLatLng = {} allPurchases = [] for purchase in purchaseData: purchaseObject = {} purchaseInfo = parsePurchase(purchase) merchantId = purchaseInfo["merchantId"] merchantInfo = parseMerchant(merchantId) lat = merchantInfo["lat"] lng = merchantInfo["lng"] purchaseObject["name"] = merchantInfo["name"] purchaseObject["amountSpent"] = purchaseInfo["amountSpent"] purchaseObject["date"] = purchaseInfo["date"] purchaseObject["category"] = merchantInfo["category"] allPurchases.append(purchaseObject) code = geohash2.encode(lat, lng) code = code[:-8] if code in locationFrequency: locationFrequency[code] += 1 decodedLatLng[geohash2.decode(code)] += 1 else: locationFrequency[code] = 1 decodedLatLng[geohash2.decode(code)] = 1 out = sorted(decodedLatLng, reverse=True, key=decodedLatLng.get) locations = out[0:5] outputData = parseGroupon(locations) return json.dumps(outputData)
def __init__(self, packet): self.latitude = packet["decoded"]["data"]["position"]["latitude"] self.longitude = packet["decoded"]["data"]["position"]["longitude"] self.altitude = packet["decoded"]["data"]["position"]["altitude"] self.node_id = packet["from"] self.geohash = geohash2.encode(self.latitude, self.longitude) self.rx_snr = None if ("rxSnr" in packet): self.rx_snr = packet["rxSnr"]
def geohash_encode(latitude, longitude): """ Compute Geohash from coordinates. -- https://en.wikipedia.org/wiki/Geohash """ geohash = geohash2.encode(float(latitude), float(longitude)) # Eight characters should be fine? #geohash = geohash[:8] return geohash
def get_neighbors(self, geohash_string): """ Returns the geohashes of the neighbors Tiles. Adapted/copied from https://github.com/tammoippen/geohash-hilbert/blob/master/geohash_hilbert/_utils.py :param geohash_string: str :return: dict """ lat, lon, lat_err, lon_err = geohash.decode_exactly(geohash_string) precision = len(geohash_string) north = lat + 2 * lat_err south = lat - 2 * lat_err east = lon + 2 * lon_err if east > 180: east -= 360 west = lon - 2 * lon_err if west < -180: west += 360 neighbours_dict = { 'east': geohash.encode(lat, east, precision), 'west': geohash.encode(lat, west, precision), } if north <= 90: # input cell not already at the north pole neighbours_dict.update({ 'north': geohash.encode(north, lon, precision), 'north-east': geohash.encode(north, east, precision), 'north-west': geohash.encode(north, west, precision), }) if south >= -90: # input cell not already at the south pole neighbours_dict.update({ 'south': geohash.encode(south, lon, precision), 'south-east': geohash.encode(south, east, precision), 'south-west': geohash.encode(south, west, precision), }) return neighbours_dict
def get_forecast_from_db(lat, lon, date_time_str, run): firestore_client = firestore.Client() forecasts = (firestore_client.collection("forecast_run").document( run).collection("forecast_date").document( date_time_str).collection("forecast_geohash").document( geohash.encode(lat, lon, 5)).collection("forecast_geopoint").get()) for forecast in forecasts: forecast_dict = forecast.to_dict() if forecast_dict["lat"] == lat and forecast_dict["lon"] == lon: return forecast_dict return None
def prepro_data(file_path, ret_cont = "req_serials", time_itl=60 * 60): file_15_1 = open(file_path, 'r') reader = csv.DictReader(file_15_1) orders = [] num_itl = int(24 * 60 * 60 / time_itl) my_sta_set = set() pre_num = 25 cnt_w = [0] * 7 cnt_day = [] y_day2w_day = {} max_time_idx = 0 min_time_idx = int(1e9) for item in reader: if item['pickup_longitude'] != '0': _item0 = dict() _item0['S_geohash'] = geohash2.encode(float(item['pickup_longitude']), float(item['pickup_latitude']), 5) my_sta_set.add(_item0['S_geohash']) St = time.strptime(item['tpep_pickup_datetime'], '%Y-%m-%d %H:%M:%S') Tt = time.strptime(item['tpep_dropoff_datetime'], '%Y-%m-%d %H:%M:%S') idx_np = idx_time(St.tm_yday, time_cnt(St, time_itl), num_itl) y_day2w_day[St.tm_yday] = St.tm_wday min_time_idx = min(min_time_idx, idx_np) max_time_idx = max(max_time_idx, idx_np) _item0['St_week_day'] = St.tm_wday _item0['St_year_day'] = St.tm_yday _item0['S_time'] = time_cnt(St, time_itl) _item0['T_time'] = time_cnt(Tt, time_itl) _item0['St'] = St _item0['Tt'] = Tt _item0['dist'] = float(item['trip_distance']) orders.append(_item0) if ret_cont == "req_serials": req_serials = {item: [0 for i in range(max_time_idx - min_time_idx + 1)] for item in my_sta_set} for item in orders: day = item['St_year_day'] time_day = item['S_time'] _idx_time = idx_time(day, time_day, num_itl) - min_time_idx req_serials[item['S_geohash']][_idx_time] += 1 return req_serials, my_sta_set
def mapper(self, _, line): trip_variance = 0 for row in csv.reader([line]): #storing coordinates pickup_lat = row[17] pickup_lon = row[18] dropoff_lat = row[20] dropoff_lon = row[21] trip_seconds = row[4] if trip_seconds == 'Trip Seconds': #Skips header final_geohash = None trip_seconds = 0 continue if not pickup_lat or not pickup_lon \ or not dropoff_lat or not dropoff_lon: #skips rows with empty cells final_geohash = None trip_seconds = 0 continue time_of_day = process_time(row) #Computing final geohash and variance if time_of_day: try: pickup_geohash = geohash2.encode(float(pickup_lat), float(pickup_lon), 7) dropoff_geohash = geohash2.encode(float(dropoff_lat), float(dropoff_lon), 7) final_geohash = pickup_geohash + "_" + dropoff_geohash + "_" + time_of_day trip_variance = abs((float(trip_seconds) - float(self.d[final_geohash]))**2) except: final_geohash = None trip_variance = 0 else: final_geohash = None trip_variance = 0 yield final_geohash, (trip_variance, 1)
def ZipToGeohash2(self, zipcode, precision=4): ''' Use pgeocode to transfer latitude and longitude to zipcode Use geohash2 to transfer zipcode to geohash precision: define the precision of geohash ''' nomi = pgeocode.Nominatim('us') info=nomi.query_postal_code(zipcode) latitude, longitude=info.latitude, info.longitude try: geohash=geohash2.encode(info.latitude, info.longitude, precision) except: geohash=None #in case there are no zip code for input data return geohash
def get_demand(df, list_lat, list_long, list_period): list_geohash = [ geohash2.encode(lat, long, 6) for lat, long in zip(list_lat, list_long) ] query = df[(df['Period'].isin(list_period)) & (df['geohash6'].isin(list_geohash))] query = query.set_index(['geohash6', 'Period'])['demand'].to_dict() demands = [] for geohash, per in zip(list_geohash, list_period): if (geohash, per) in query.keys(): demands.append(query[(geohash, per)]) else: demands.append(0) return demands
def geo_info(qrz, a_callsign, a_state, states_coords): try: qrz_result = qrz.callsign(a_callsign) except: try: a_latitude = states_coords[a_state][0] a_longitude = states_coords[a_state][1] except: a_latitude = 0.0 a_longitude = 0.0 else: a_latitude = float(qrz_result['lat']) a_longitude = float(qrz_result['lon']) a_geohash = geohash2.encode(a_latitude, a_longitude, precision=5) return (a_latitude, a_longitude, a_geohash)
def _update_info(self, now=None): self.service.update() for person in self.service.people: try: dev_id = person.id except TypeError: log.warning("No location(s) shared with this account") return if self.max_gps_accuracy is not None and person.accuracy > self.max_gps_accuracy: log.info( 'Ignoring {} update because expected GPS accuracy {} is not met: {}' .format(person.first_name, self.max_gps_accuracy, person.accuracy)) continue attrs = { ATTR_ADDRESS: person.address, ATTR_FIRST_NAME: person.first_name, ATTR_FULL_NAME: person.full_name, ATTR_ID: person.id, ATTR_LAST_SEEN: self.format_datetime(person.last_seen), ATTR_GEOHASH: geohash2.encode(person.latitude, person.longitude, precision=12), ATTR_BATTERY_CHARGING: person.battery_charging, ATTR_BATTERY_LEVEL: person.battery_level } self.see(dev_id='{}_{}'.format(person.first_name, str(person.id)[-8:]), gps=(person.latitude, person.longitude), picture=person.picture_url, source_type=SOURCE_TYPE_GPS, host_name=person.first_name, gps_accuracy=person.accuracy, attributes=attrs)
def get_geo_hashes_map(latitude, longitude): """ This function, given a coordinates (lat,long), calculates the geohash and builds a map containing a geohash in different lengths. Args: coordinates: Coordinates in the format {'latitude': float, 'longitude': float}. Returns: A dict contaning geohash in all the diffrent lengths e.g.: {'hash1':'d', 'hash2':'dp', 'hash3':'dph', ... 'hash10':'dph1qz7y88',}. """ geo_hashes_map = {} geohash = geohash2.encode(latitude, longitude) for i in range(1, 12): geo_hashes_map['hash' + str(i)] = geohash[0:i] return geo_hashes_map
def getResult(location, length): try: # location = input.replace('#','') querystr = '/geocoder/v2/?address=' + location + '&output=json&ak=' + ak # 对queryStr进行转码,safe内的保留字符不转换 encodedstr = urllib.parse.quote(querystr, safe="/:=&?#+!$,;'@()*[]") # encodedStr = urllib.parse.quote(queryStr) rawstr = encodedstr + sk sn = hashlib.md5(urllib.parse.quote_plus(rawstr).encode("utf-8")).hexdigest() url2 = "http://api.map.baidu.com/geocoder/v2/?address={0}&output=json&ak={1}&sn={2}".format(location, ak, sn) ret = requests.get(url2).text ret_dic = eval(ret) lng = ret_dic['result']['location']['lng'] lat = ret_dic['result']['location']['lat'] geohash = geo.encode(lng, lat) print(str(location) + "----"+ str(geohash)) except: geohash = "-" return geohash
def _encode(lat, lon, precision=15): """ Encodes latitude/longitude to geohash, either to specified precision or to automatically evaluated precision. Parameters ---------- lat : number Latitude in degrees. lon : number Longitude in degrees. precision : number, optional, default 15 Number of characters in resulting geohash. Return ------ string Geohash of supplied latitude/longitude. """ return gh.encode(lat, lon, precision)
def _encode(lat: float, lon: float, precision: Optional[float] = 15) -> Text: """ Encodes latitude/longitude to geohash, either to specified precision or to automatically evaluated precision. Parameters ---------- lat : float Latitude in degrees. lon : float Longitude in degrees. precision : float, optional Number of characters in resulting geohash, by default 15 Return ------ str Geohash of supplied latitude/longitude. """ return gh.encode(lat, lon, precision)