def _add_document(element, provider, job_name, doc_ref): geo_point_coordinates = firestore.GeoPoint(element.latitude, element.longitude) geo_hashes_map = get_geo_hashes_map(element.latitude, element.longitude) doc_ref.set({ database_schema.COLLECTION_IMAGES_FIELD_ADDITIONAL_URL: element.additional_url, database_schema.COLLECTION_IMAGES_FIELD_PASSED_FILTER: element.passed_filter, database_schema.COLLECTION_IMAGES_FIELD_URL: element.url, database_schema.COLLECTION_IMAGES_FIELD_INGESTED_PROVIDERS: [provider.provider_id], database_schema.COLLECTION_IMAGES_FIELD_INGESTED_RUNS: [job_name], database_schema.COLLECTION_IMAGES_FIELD_COORDINATES: geo_point_coordinates, database_schema.COLLECTION_IMAGES_FIELD_DATE_INGESTED: datetime.now(), database_schema.COLLECTION_IMAGES_FIELD_DATE_SHOT: element.date_shot, database_schema.COLLECTION_IMAGES_FIELD_DATE_FIELDS: _get_date_fields(element.date_shot), database_schema.COLLECTION_IMAGES_FIELD_HASHMAP: geo_hashes_map, database_schema.COLLECTION_IMAGES_FIELD_IMAGE_ATTRIBUTES: { database_schema.COLLECTION_IMAGES_FIELD_FORMAT: element.format, database_schema.COLLECTION_IMAGES_FIELD_RESOLUTION: { database_schema.COLLECTION_IMAGES_FIELD_WIDTH: element.width_pixels, database_schema.COLLECTION_IMAGES_FIELD_HEIGHT: element.height_pixels, }, }, database_schema.COLLECTION_IMAGES_FIELD_ATTRIBUTION: element.attribution, database_schema.COLLECTION_IMAGES_FIELD_RANDOM: random.random(), database_schema.COLLECTION_IMAGES_FIELD_VISIBILITY: VisibilityType.INVISIBLE.value, })
def test_document_get(client, cleanup): now = datetime.datetime.utcnow().replace(tzinfo=UTC) document_id = "for-get" + unique_resource_id("-") document = client.document("created", document_id) # Add to clean-up before API request (in case ``create()`` fails). cleanup(document) # First make sure it doesn't exist. assert not document.get().exists ref_doc = client.document("top", "middle1", "middle2", "bottom") data = { "turtle": "power", "cheese": 19.5, "fire": 199099299, "referee": ref_doc, "gio": firestore.GeoPoint(45.5, 90.0), "deep": [u"some", b"\xde\xad\xbe\xef"], "map": { "ice": True, "water": None, "vapor": { "deeper": now } }, } write_result = document.create(data) snapshot = document.get() check_snapshot(snapshot, document, data, write_result)
def test_document_get(client, cleanup): now = datetime.datetime.utcnow().replace(tzinfo=UTC) document_id = 'for-get' + unique_resource_id('-') document = client.document('created', document_id) # Add to clean-up before API request (in case ``create()`` fails). cleanup(document) # First make sure it doesn't exist. assert not document.get().exists ref_doc = client.document('top', 'middle1', 'middle2', 'bottom') data = { 'turtle': 'power', 'cheese': 19.5, 'fire': 199099299, 'referee': ref_doc, 'gio': firestore.GeoPoint(45.5, 90.0), 'deep': [ u'some', b'\xde\xad\xbe\xef', ], 'map': { 'ice': True, 'water': None, 'vapor': { 'deeper': now, }, }, } write_result = document.create(data) snapshot = document.get() check_snapshot(snapshot, document, data, write_result) assert_timestamp_less(snapshot.create_time, snapshot.read_time)
def parse_and_insert_airports(path): airports_ref = db.collection('airports') count = 0 with open(path, newline='', encoding='utf-8') as airports_file: airports = csv.DictReader(airports_file) for airport in airports: if 'airport' not in airport['type']: continue code = airport['iata_code'] or airport['local_code'] if not code: continue count += 1 if count <= 20100: continue name = airport['name'] coords = firestore.GeoPoint(float(airport['latitude_deg']), float(airport['longitude_deg'])) location = '{}, {}'.format( airport['municipality'], airport['iso_country']) # TODO: Parse this and iso_region airports_ref.document(code).set({ 'coordinates': coords, 'location': location, 'name': name }) # count += 1 if count % 100 == 0: print('Added ', count)
def create_food_bank(request, lat, long): pass # 34.040670, -118.255870 # db. location = firestore.GeoPoint(lat, long) doc_ref = db.collection("food_banks").document() doc_ref.set({"name": "i love free stuff", "location": location}) return HttpResponse("sure")
def printdata(entrybox):#Entrybox which takes the input cordinate from the user text = entrybox.get() print(str(text)) l=[] # list to store x and y cordinated at the index 0 and one respectively for cordinate in text.split(','): l.append(float(cordinate)) self.data['location']= firestore.GeoPoint(float(l[0]), float(l[1])) print(self.data)
def loginUser(user): ## user -> {email: string, location: geopoint, name: string, phone: string, likes: string list} ## TODO: change to get details from Google SSO db = firestore.Client() location = user['location'].split(',') doc_ref = db.collection(u'users').document(user['email']) if (doc_ref.get().exists): #update location doc_ref.update({u'location': firestore.GeoPoint(float(location[0]), float(location[1]))}) else: # create user doc_ref.set({ u'email': user['email'], u'location': firestore.GeoPoint(float(location[0]), float(location[1])), u'name': user['name'] # u'phone': user['phone'], # u'likes': user['likes'].split(',') })
def use_firestore_types(key: K, value: Dict[str, Any]) -> Tuple[K, Dict[str, Any]]: """The pipeline itself should be independent from Firestore types because that is just one possible sink. That's why this function here should be called just before writing to Firestore.""" geopoint: Optional[GeoPoint] = value.get(fields.GEOPOINT) if geopoint: value = copy.copy(value) value[fields.GEOPOINT] = firestore.GeoPoint( latitude=geopoint.latitude, longitude=geopoint.longitude) return key, value
def locate(news_lst, city) : '''Temporary geohasher assigns random locations based on city parameter; also assigns unique id's ARGS --- news_lst : list of news articles in format ... city (string) : collection id RETURNS --- list of news articles in format ... ''' #long beach values if (city == "long-beach"): max_lat = 33.8765 min_lat = 33.7664 max_lon = -118.0997 min_lon = -118.2042 #cerritos values elif (city == "cerritos"): max_lat = 33.8879 min_lat = 33.8459 max_lon = -118.0287 min_lon = -118.1085 # #bellflower values elif (city == "bellflower"): max_lat = 33.9105 min_lat = 33.8656 max_lon = -118.1067 min_lon = -118.1514 # #lakewood values elif (city == "lakewood"): max_lat = 33.8692 min_lat = 33.8202 max_lon = -118.0590 min_lon = -118.1677 rand.seed() # for item in news_lst : lati = min_lat + (max_lat-min_lat)*rand.random() lon = min_lon + (max_lon-min_lon)*rand.random() ghash = geohash.encode(lati, lon, 7) loncoord = float(geohash.decode(ghash).lon) latcoord = float(geohash.decode(ghash).lat) item["geohash"] = ghash item["id"] = gen_id(item) item["coordinates"] = firestore.GeoPoint(latcoord, loncoord) return news_lst
def push(self): # Get a ref to Firestore database. maps_collection = self.db.collection('maps') # This is just for logging purposes. total = len(self.maps_db) idx = 0 # Start a batch batch = self.db.batch() for nts_map in self.maps_db: # Derive map type if len(nts_map['name']) == 3: nts_map['map_type'] = 'series' elif len(nts_map['name']) == 4: nts_map['map_type'] = 'area' else: nts_map['map_type'] = 'sheet' # Derive centre coordinates centerLat = (nts_map['north'] + nts_map['south']) / 2 centerLng = (nts_map['east'] + nts_map['west']) / 2 nts_map['center'] = firestore.GeoPoint(centerLat, centerLng) # Derive geohash nts_map['geohash'] = pgh.encode(centerLat, centerLng) # Commit the batch at every 500th record. if idx % 500 == 0: if idx > 0: print('Committing..') batch.commit() # Start a new batch for the next iteration. batch = self.db.batch() idx += 1 print( str(idx) + str('/') + str(total) + ': ' + str(nts_map['name']) + ' (Geohash = ' + nts_map['geohash'] + ')') record_ref = maps_collection.document(nts_map['name']) # Include current record in batch batch.set(record_ref, nts_map) # Include current record in batch if idx % 500 != 0: print('Committing..') batch.commit()
def createSystemEvent(category, event, timetag): ## event = {name: string, details: string, location-name: location-coords: geopoint, min: number, max: number, datetime: timestamp, status: string, participants: map, category: string} db = firestore.Client() d = datetime.datetime.now() time_tag_split = timetag.split('-') d = datetime.datetime(d.year, d.month, d.day, time_of_day[time_tag_split[1]], 0, 0) next_dt = next_weekday(d, days[time_tag_split[0]]) # print("next_dt ", next_dt) # eventdt = str(next_dt)[0:11]+'17'+str(next_dt)[13:19] # user_ref = db.collection(u'users').document(user) # location = event['location'].split(',') # doc_ref = db.collection(u'events').document(category+ " @ " +event['name']) a, b = db.collection(u'events').add({ u'name': category + " @ " + event['name'], u'location_name': event['name'], u'category': category, u'details': "Address : " + event['address'], u'location_coords': firestore.GeoPoint(float(event['found_loc_coords'][0]), float(event['found_loc_coords'][1])), # u'min': int(event['min']), # u'max': int(event['max']), u'datetime': next_dt, # u'datetime': datetime.datetime.now() + datetime.timedelta(days=3), u'status': 'scheduled', u'is_active': True, u'created_by': 'System Bot', u'confirmed_participants': event['probCandidates'] }) print("printing Event id " + b.id) for each_request_id in event['request_ids']: print(each_request_id) query = db.collection(u'user_requests').document( each_request_id).update({u'event_id': b.id}) addNotifications(event['request_ids'], b)
def createUserInterest(user_ref, interest): ## user -> user-reference ## interest -> {category: <string>, location: <geopoint>, radius: <number>, time-tag: <string: 'sat-morn'>, user : <user-ref>} db = firestore.Client() #user_ref = helpers.getUserFromAuthHeader(user_auth) location = interest['location'].split(',') db.collection(u'user_requests').add({ u'category': interest['category'], #u'is_active': True, u'location': firestore.GeoPoint(float(location[0]), float(location[1])), u'radius': float(interest['radius']), u'time_tag': interest['time_tag'], u'event_id': "", u'user': user_ref })
def get_query_from_heatmap_collection(db, label, quantized_coords): """ Returns a query from database_schema.COLLECTION_HEATMAP according to the arguments. Args: label: Label id to query by. quantized_coords: the quantized coordinates to query by. """ quantized_coords_lat = quantized_coords[0] quantized_coords_lng = quantized_coords[1] geopoint_quantized_coords = firestore.GeoPoint(\ quantized_coords_lat, quantized_coords_lng) query = db.collection_group(\ database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS).\ where(database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS_FIELD_COORDINATES,\ u'==', geopoint_quantized_coords).\ where(database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS_FIELD_LABEL_ID,\ u'==', label).stream() return query
def add_point_key_to_heatmap_collection(db, quantized_coords, precision, label, count): """ Adds a new point key to database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS. The point key will be added to the relevant precision document. """ precision_string_id = 'precision{precision_number}'.format(precision_number=precision) quantized_coords_lat = quantized_coords[0] quantized_coords_lng = quantized_coords[1] geopoint_quantized_coords = firestore.GeoPoint(quantized_coords_lat, quantized_coords_lng) db.collection(database_schema.COLLECTION_HEATMAP).document(precision_string_id).\ collection(database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS).\ document().set({ database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS_FIELD_LABEL_ID:\ label, database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS_FIELD_COORDINATES:\ geopoint_quantized_coords, database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS_FIELD_WEIGHT:\ count, database_schema.COLLECTION_HEATMAP_SUBCOLLECTION_WEIGHTED_POINTS_FIELD_HASHMAP:\ get_geo_hashes_map(quantized_coords_lat, quantized_coords_lng) })
def createEvent(user_ref, event): ## event -> {name: string, details: string, location-name: location-coords: geopoint, min: number, max: number, datetime: timestamp, status: string, participants: list, category: string} ## TODO: datetime db = firestore.Client() #user_ref = helpers.getUserFromAuthHeader(user_ref) location = event['location'].split(',') db.collection(u'events').add({ u'name': event['name'], u'category': event['category'], u'details': event['details'], u'location_name': event['location_name'], u'location_coords': firestore.GeoPoint(float(location[0]), float(location[1])), u'min': int(event['min']), u'max': int(event['max']), u'datetime': helpers.parseDateTimeFromString(event['datetime']), u'status': 'scheduled', u'is_active': True, u'created_by': user_ref, u'confirmed_participants': [user_ref] })
def cvt2GeoPoint(lat, lon): return fs.GeoPoint(lat, lon)
def Add_Coords(lon, lat): # Add a new document db = firestore.Client() doc_ref = db.collection(u'Managers').document( u'GaYbpcstLnf0uAcYfWSAJG7Rudt2').collection(u'Locations') doc_ref.add({u'co-ords': firestore.GeoPoint(lon, lat)})
def GeoPoint(latitude: float, longitude: float): return firestore.GeoPoint(latitude, longitude)
import time import random import firebase_admin from firebase_admin import credentials from firebase_admin import firestore import google.cloud.firestore as fs cred = credentials.Certificate('./adminsdk.json') firebase_admin.initialize_app(cred) db = firestore.client() while True: crawler_ref = db.collection(u'events') lat = random.uniform(51.035, 51.037) long = random.uniform(-114.090, -114.093) new_obj_ref = crawler_ref.add( {'location': fs.GeoPoint(latitude=lat, longitude=long)}) time.sleep(1)
def parse(self): return firestore.GeoPoint(self.latitude, self.longitude)