def get_mileage(self, lat, lon): line = LineString([(pt.lon, pt.lat) for pt in self.path_points]) point_on_segment = nearest_points(line, Point(lon, lat))[0] i, _ = self.nearest_segment(lat, lon) return self.path_point_mileages[i] + self.distance( Coordinates(point_on_segment.y, point_on_segment.x), self.path_points[i])
def nearest_segment(self, lat, lon): dists = { i: self.distance(Coordinates(lat, lon), pt) for i, pt in enumerate(self.path_points) } nearest_idx = min(dists.keys(), key=lambda i: dists[i]) del dists[nearest_idx] second_nearest_idx = min(dists.keys(), key=lambda i: dists[i]) assert abs(nearest_idx - second_nearest_idx) == 1 # expecting adjacent index return (min(nearest_idx, second_nearest_idx), max(nearest_idx, second_nearest_idx))
async def nearby_stops(lat: float, lon: float, radius: float, pool) -> List[Stop]: async with pool.acquire() as conn: query = f"""select q.lat, q.lon from (select (st_distance(st_transform(st_setsrid(s.geopoint, 4326)::geometry, 3857), st_transform(ST_SetSRID(st_makepoint({lon}, {lat}), 4326)::geometry, 3857))) as d, s.lat, s.lon from stop s ) as q where q.d < {radius}; """ rows = await conn.fetch(query) return [ Stop(Coordinates(float(r['lat']), float(r['lon'])), arrivals=[]) for r in rows ]
def init_db(db_session): # Tables should be created with Alembic migrations # But if you don't want to use migrations, create # the tables un-commenting the next line Base.metadata.create_all(bind=engine) for user_fill in fill_data['users']: user = crud.user.get_by_email(db_session, email=user_fill['email']) if not user: user_in = UserCreate(email=user_fill['email'], password=user_fill['password'], full_name=user_fill['name'], is_superuser=user_fill['is_superuser'], can_report=user_fill['can_report'], relation=user_fill['relation']) user = crud.user.create(db_session, user_in=user_in) for sujeto_fill in fill_data['sujetos']: sujeto = crud.sujeto.get_by_name(db_session, name=sujeto_fill['name']) if not sujeto: sujeto_in = SujetoCreate( name=sujeto_fill['name'], gender=sujeto_fill['gender'], apodos=sujeto_fill['apodos'], birth=datetime.datetime.strptime(sujeto_fill['birth'], '%Y-%m-%d %H:%M:%S') if sujeto_fill['birth'] is not None else None, ) sujeto = crud.sujeto.create(db_session, sujeto_in=sujeto_in) for location_fill in fill_data['locations']: location = crud.location.get_by_name( db_session, location_name=location_fill['name']) if not location: location_in = LocationCreate(name=location_fill['name'], center=Coordinates( lat=location_fill['lat'], lng=location_fill['lng']), radius=location_fill['radius']) location = crud.location.create(db_session, location_in=location_in)
def insert_location( *, body: dict, db_session: Session = Depends(get_db), ): log.debug(body) if body["api_key"] != config.TASKER_API_KEY: log.debug(f"Location recived with api_key {body['api_key']}.") return {"msg": "Not authorized."} log.debug(body) apodo = body["sujeto"] lat, lng = body["location"].split(",") lat = float(lat) lng = float(lng) sujeto = crud.sujeto.get_by_apodo(db_session=db_session, apodo=apodo) coordinates = Coordinates(lat=lat, lng=lng) crud.sujeto.update_latlng(db_session=db_session, sujeto=sujeto, coordinates=coordinates, car=body["car"]) log.debug(f"Location updated for user {sujeto.name}") return {"msg": "Location updated."}
if not location: location_in = LocationCreate(name=location_fill['name'], center=Coordinates( lat=location_fill['lat'], lng=location_fill['lng']), radius=location_fill['radius']) location = crud.location.create(db_session, location_in=location_in) from db.session import db_session init_db(db_session) external_data.download_who_data() sujeto = crud.sujeto.get_by_apodo(db_session=db_session, apodo="Casa") coordinates = Coordinates(lat=41.582629, lng=1.628431) crud.sujeto.update_latlng(db_session=db_session, sujeto=sujeto, coordinates=coordinates, car=False) # sujeto=crud.sujeto.get_by_name(db_session=db_session, name="Entropía") # coordinates = Coordinates(lat=41.582603, lng=1.628425) # crud.sujeto.update_latlng(db_session=db_session, sujeto=sujeto, coordinates=coordinates, car=False) # sujeto=crud.sujeto.get_by_apodo(db_session=db_session, apodo="Ausias") # coordinates = Coordinates(lat=41.549812, lng=1.847321) # crud.sujeto.update_latlng(db_session=db_session, sujeto=sujeto, coordinates=coordinates, car=True) # sujeto=crud.sujeto.get_by_apodo(db_session=db_session, apodo="Casa") # coordinates = Coordinates(lat=41.582629, lng=1.628431) # crud.sujeto.update_latlng(db_session=db_session, sujeto=sujeto, coordinates=coordinates, car=False) # sujeto=crud.sujeto.get_by_apodo(db_session=db_session, apodo="Carla")
def linestring_to_coords(linestring: str) -> List[Coordinates]: parsed = wkb.loads(linestring, hex=True) return [ Coordinates(lat, lon) for lon, lat in zip(parsed.xy[0], parsed.xy[1]) ]
def get_coordinates_from_geom(geom: WKTElement) -> Coordinates: sahply_geom = to_shape(geom) coordinates = Coordinates(lng=sahply_geom.x, lat=sahply_geom.y) return coordinates