Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new end point that determines a road type according to lon and lat #1583

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
from anyway.views.news_flash.api import (
news_flash,
single_news_flash,
get_road_type
)

app.config.from_object(__name__)
Expand Down Expand Up @@ -1464,7 +1465,7 @@ def logout():
login.logout_user()
return redirect(url_for("index"))


#---------------------------------api------------------------------------------------------------
app.add_url_rule("/api/schools", endpoint=None, view_func=schools_api, methods=["GET"])
app.add_url_rule(
"/api/schools-description", endpoint=None, view_func=schools_description_api, methods=["GET"]
Expand Down Expand Up @@ -1499,7 +1500,9 @@ def logout():
)
app.add_url_rule("/api/news-flash", endpoint=None, view_func=news_flash, methods=["GET"])

app.add_url_rule("/api/road-type/<float:lat>/<float:lon>", endpoint=None, view_func=get_road_type, methods=["GET"])

#---------------------------------api------------------------------------------------------------
@app.route("/authorize/<provider>")
def oauth_authorize(provider):
if not current_user.is_anonymous:
Expand Down
64 changes: 64 additions & 0 deletions anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TIMESTAMP,
)
import sqlalchemy
import geoalchemy2
from sqlalchemy.orm import relationship, load_only, backref
from sqlalchemy import or_, and_

Expand Down Expand Up @@ -791,6 +792,68 @@ def is_anonymous(self):
def get_id(self):
return self.id

class WazeAllerts(Base):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waze alerts table exists (See class WazeAlert in this file)
What there's a need in another one?

__tablename__ = "waze_alerts"

alert_subtype = Column(BigInteger(), nullable=True)
alert_type = Column(Text(), nullable=True)
back_filled = Column(Boolean(), nullable = True)
city = Column(Text(), nullable = True)
confidence = Column(Integer(), nullable = True)
created_at = Column(DateTime(), nullable = True)
ended_at_estimate = Column(DateTime(), nullable = True)
geom = Column(geoalchemy2.types.Geometry(geometry_type='LINESTRING', from_text='ST_GeomFromEWKT', name='geometry'), nullable=True)
id = Column(BigInteger(), nullable=False,primary_key=True)
jam_uuid = Column(Text(), nullable=True)
latitude = Column(Float(), nullable=True)
longitude = Column(Float(), nullable=True)
magvar = Column(Integer(), nullable=True)
number_thumbs_up = Column(Integer(), nullable=True)
reliability = Column(Integer(), nullable=True)
report_by_municipality_user = Column(Boolean(), nullable=True)
report_description = Column(Text(), nullable=True)
report_rating = Column()
road_type = Column(Integer(), nullable=True)
street = Column(Text(), nullable=True)
uuid = Column(Text(), nullable=True)
def serialize(self):
return {
"alert_subtype": self.alert_subtype,
"alert_type": self.alert_type,
"back_filled": self.back_filled,
"city": self.city,
"confidence": self.confidence,
"created_at": self.created_at,
"ended_at_estimate": self.ended_at_estimate,
"geom": self.geom,
"id": self.id,
"jam_uuid": self.jam_uuid,
"latitude": self.latitude,
"longitude": self.longitude,
"magvar": self.magvar,
"number_thumbs_up": self.number_thumbs_up,
"reliability": self.reliability,
"report_by_municipality_user": self.report_by_municipality_user,
"report_description": self.report_description,
"report_rating": self.report_rating,
"road_type": self.road_type,
"street": self.street,
"uuid": self.uuid
}

# Flask-Login integration
def is_authenticated(self):
return True

def is_active(self):
return True

def is_anonymous(self):
return False

def get_id(self):
return self.id


class NewsFlash(Base):
__tablename__ = "news_flash"
Expand Down Expand Up @@ -2069,6 +2132,7 @@ class InvolvedMarkerView(Base):

class WazeAlert(Base):
__tablename__ = "waze_alerts"
__table_args__ = {'extend_existing': True}

id = Column(BigInteger(), primary_key=True)
city = Column(Text())
Expand Down
15 changes: 15 additions & 0 deletions anyway/views/news_flash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from anyway.backend_constants import BE_CONST
from anyway.base import user_optional
from anyway.models import NewsFlash
from anyway.models import WazeAllerts



@user_optional
Expand Down Expand Up @@ -103,3 +105,16 @@ def single_news_flash(news_flash_id: int):
json.dumps(news_flash_obj.serialize(), default=str), mimetype="application/json"
)
return Response(status=404)
from sys import maxsize
@user_optional
def get_road_type(lat: float, lon: float):
offset = 0.00001
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you decide on the offset size (0.00001)?
In general - I suggest using geographical query instead of the query below -see geom.intersects here

In this case - I think that using both the geographical query and the road_number / street name together can be very powerful - just using one of them is not enought.
However - we can start with geographical query and improve it.

result = db.session.query(WazeAllerts).filter(
and_(WazeAllerts.latitude <= lat + offset,
WazeAllerts.latitude >= lat - offset,
WazeAllerts.longitude <= lon + offset,
WazeAllerts.longitude >= lon - offset)).first()

return Response(
json.dumps(result.road_type, default=str), mimetype="application/json"
)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ urllib3==1.25.9
webassets==2.0
xlrd==1.2.0
lxml==4.5.1
boto3==1.14.48
boto3==1.14.48