def load_markers(file): """Load markers from dataset into Marker database.""" with open(file) as csvfile: # next(f) # skip header row # for i, row in enumerate(f): # row = row.rstrip() # print row.split("\t") # 0)title, 1)description, 2)date, 3)date-tier, 4)time, # 5)name(venue), 6)foursquare_id, 7)neighborhood, 8)city, 9)address, # 10)latitude, 11)longitude, 12)cost, 13)img_url, 14)event_url, # 15)category, 16)marker_type, 17)marker_symbol, 18)marker_color, # 19) datetime_obj (new cell) csvreader = csv.reader(csvfile) # Skips the first header row of the CSV file next(csvreader) for i, row in enumerate(csvreader): if row[2]: date_str = str(row[2]) datetime_obj = datetime.strptime(date_str, "%B %d, %Y") else: datetime_obj = None marker = Marker(title=row[0], address=row[9], latitude=row[10], longitude=row[11], date=row[2], date_tier=row[3], time=row[4], name=row[5], neighborhood=row[7], city=row[8], description=row[1], cost=row[12], img_url=row[13], event_url=row[14], category=row[15], marker_type=row[16], marker_symbol=row[17], marker_color=row[18], datetime=datetime_obj, foursquare_id=row[6]) db.session.add(marker) if i % 10 == 0: print i db.session.commit()
def add_new_location(): """Add user's new favorite location to map.""" user_id = session.get('user_id') location_id = int(request.form.get('location_id')) new_fav_loc = Fav_Loc(user_id=user_id, location_id=location_id) db.session.add(new_fav_loc) db.session.commit() location = Location.query.filter(Location.location_id == location_id).first() new_marker = Marker(location.location_name, location.longitude, location.latitude, location.location_id) marker_list = [] marker_geojson = new_marker.generate_geojson() marker_list.append(marker_geojson) marker_collection = geojson.FeatureCollection(marker_list) return jsonify(location_name=location.location_name, marker_collection=marker_collection)
def show_map(): """Renders main map view. Queries database for user's favorite locations and makes geoJSON feature collection to put markers on map.""" # print session user_id = session.get('user_id') user = User.query.filter_by(user_id=user_id).first() fav_loc = db.session.query(Location).join(Fav_Loc).filter(Fav_Loc.user_id == user_id).all() marker_list = [] locations = Location.query.all() for location in fav_loc: marker = Marker(location.location_name, location.longitude, location.latitude, location.location_id) marker_geojson = marker.generate_geojson() marker_list.append(marker_geojson) marker_collection = geojson.FeatureCollection(marker_list) return render_template("homepage.html", locations=locations, marker_collection=marker_collection, user=user, fav_loc=fav_loc)