Skip to content

Commit

Permalink
remove unused functions and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Elazar Gershuni committed Aug 2, 2023
1 parent 812cfc7 commit 4cdd88c
Show file tree
Hide file tree
Showing 16 changed files with 7 additions and 359 deletions.
21 changes: 0 additions & 21 deletions anyway/globalmaptiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,6 @@ def PixelsToTile(self, px, py):
ty = int(math.ceil(py / float(self.tileSize)) - 1)
return tx, ty

def PixelsToRaster(self, px, py, zoom):
"Move the origin of pixel coordinates to top-left corner"

mapSize = self.tileSize << zoom
return px, mapSize - py

def MetersToTile(self, mx, my, zoom):
"Returns tile for given mercator coordinates"

Expand Down Expand Up @@ -249,13 +243,6 @@ def Resolution(self, zoom):
# return (2 * math.pi * 6378137) / (self.tileSize * 2**zoom)
return self.initialResolution / (2 ** zoom)

def ZoomForPixelSize(self, pixelSize):
"Maximal scaledown zoom of the pyramid closest to the pixelSize."

for i in range(30):
if pixelSize > self.Resolution(i):
return i - 1 if i != 0 else 0 # We don't want to scale up

def GoogleTile(self, tx, ty, zoom):
"Converts TMS tile coordinates to Google Tile coordinates"

Expand Down Expand Up @@ -320,14 +307,6 @@ class GlobalGeodetic(object):
def __init__(self, tileSize=256):
self.tileSize = tileSize

def LatLonToPixels(self, lat, lon, zoom):
"Converts lat/lon to pixel coordinates in given zoom of the EPSG:4326 pyramid"

res = 180 / 256.0 / 2 ** zoom
px = (180 + lat) / res
py = (90 + lon) / res
return px, py

def PixelsToTile(self, px, py):
"Returns coordinates of the tile covering region in pixel coordinates"

Expand Down
4 changes: 0 additions & 4 deletions anyway/graphqlSchema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,5 @@ class Query(graphene.ObjectType):
node = relay.Node.Field()
accident_markers_hebrew = graphene.List(AccidentMarkerHebrew)

def resolve_accident_markers_hebrew(self, info):
query = AccidentMarkerHebrew.get_query(info)
return query.all()


schema = graphene.Schema(query=Query)
32 changes: 0 additions & 32 deletions anyway/infographics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,10 @@
ITEMS = "items"


def get_widget_factories() -> List[Type[Widget]]:
"""Returns list of callables that generate all widget instances"""
return list(widgets_dict.values())


def get_widget_class_by_name(name: str) -> Type[Widget]:
return widgets_dict.get(name)


def sum_road_accidents_by_specific_type(road_data, field_name):
dict_merge = defaultdict(int)
dict_merge[field_name] = 0
dict_merge[head_on_collisions_comparison_dict["others"]] = 0

for accident_data in road_data:
if accident_data["accident_type"] == field_name:
dict_merge[field_name] += accident_data["count"]
else:
dict_merge[head_on_collisions_comparison_dict["others"]] += accident_data["count"]
return dict_merge


def convert_roads_fatal_accidents_to_frontend_view(data_dict):
data_list = []
for key, value in data_dict.items():
# pylint: disable=no-member
if key == AccidentType.HEAD_ON_FRONTAL_COLLISION.value:
data_list.append(
{"desc": head_on_collisions_comparison_dict["head_to_head"], "count": value}
)
else:
data_list.append({"desc": key, "count": value})

return data_list


def generate_widgets_data(request_params: RequestParams) -> List[dict]:
res = []
for w in widgets_dict.values():
Expand Down
29 changes: 2 additions & 27 deletions anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class UserMixin:
from anyway import localization
from anyway.backend_constants import BE_CONST, NewsflashLocationQualification
from anyway.database import Base
from anyway.utilities import decode_hebrew

try:
from anyway.app_and_db import db
Expand Down Expand Up @@ -221,8 +220,8 @@ class MarkerMixin(Point):
def format_description(field, value):
# if the field's value is a static localizable field, fetch it.
if field in localization.get_supported_tables():
value = decode_hebrew(localization.get_field(field, value))
name = decode_hebrew(localization.get_field(field))
value = localization.get_field(field, value)
name = localization.get_field(field)
return "{0}: {1}".format(name, value)


Expand Down Expand Up @@ -1195,24 +1194,6 @@ class SuburbanJunction(Base):
nullable=True)
roads = Column(postgresql.ARRAY(Integer(), dimensions=1), nullable=False)

@staticmethod
def get_hebrew_name_from_id(non_urban_intersection: int) -> str:
res = db.session.query(SuburbanJunction.non_urban_intersection_hebrew).filter(
SuburbanJunction.non_urban_intersection == non_urban_intersection).first()
if res is None:
raise ValueError(f"{non_urban_intersection}: could not find "
f"SuburbanJunction with that symbol")
return res.non_urban_intersection_hebrew

@staticmethod
def get_id_from_hebrew_name(non_urban_intersection_hebrew: str) -> int:
res = db.session.query(SuburbanJunction.non_urban_intersection).filter(
SuburbanJunction.non_urban_intersection == non_urban_intersection_hebrew).first()
if res is None:
raise ValueError(f"{non_urban_intersection_hebrew}: could not find "
f"SuburbanJunction with that name")
return res.non_urban_intersection

@staticmethod
def get_intersection_from_roads(roads: Set[int]) -> dict:
if not all([isinstance(x, int) for x in roads]):
Expand Down Expand Up @@ -2203,9 +2184,6 @@ class RoadSegments(Base):
def get_id(self):
return self.id

def get_segment_id(self):
return self.segment_id


class ReportProblem(Base):
__tablename__ = "report_problem"
Expand Down Expand Up @@ -2860,9 +2838,6 @@ class CasualtiesCosts(Base):
year = Column(Integer())
data_source_hebrew = Column(String())

def to_str(self):
return f"{self.id}:{self.injured_type}:{self.injuries_cost_k}"


class SchoolWithDescription2020(Base):
__tablename__ = "schools_with_description2020"
Expand Down
6 changes: 0 additions & 6 deletions anyway/parsers/cbs/exceptions.py

This file was deleted.

42 changes: 1 addition & 41 deletions anyway/parsers/cbs/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
VehiclesView,
VehicleMarkerView,
)
from anyway.parsers.cbs.exceptions import CBSParsingFailed
from anyway.utilities import ItmToWGS84, time_delta, ImporterUI, truncate_tables, delete_all_rows_from_table, \
chunks, run_query_and_insert_to_table_in_chunks
from anyway.db_views import VIEWS
Expand Down Expand Up @@ -903,45 +902,6 @@ def add_suburban_junction_from_marker(marker: dict):
add_suburban_junction(j)


def delete_invalid_entries(batch_size):
"""
deletes all markers in the database with null latitude or longitude
first deletes from tables Involved and Vehicle, then from table AccidentMarker
"""

marker_ids_to_delete = (
db.session.query(AccidentMarker.id)
.filter(or_((AccidentMarker.longitude == None), (AccidentMarker.latitude == None)))
.all()
)

marker_ids_to_delete = [acc_id[0] for acc_id in marker_ids_to_delete]

logging.debug("There are " + str(len(marker_ids_to_delete)) + " invalid accident_ids to delete")

for ids_chunk in chunks(marker_ids_to_delete, batch_size):

logging.debug("Deleting a chunk of " + str(len(ids_chunk)))

q = db.session.query(Involved).filter(Involved.accident_id.in_(ids_chunk))
if q.all():
logging.debug("deleting invalid entries from Involved")
q.delete(synchronize_session="fetch")
db.session.commit()

q = db.session.query(Vehicle).filter(Vehicle.accident_id.in_(ids_chunk))
if q.all():
logging.debug("deleting invalid entries from Vehicle")
q.delete(synchronize_session="fetch")
db.session.commit()

q = db.session.query(AccidentMarker).filter(AccidentMarker.id.in_(ids_chunk))
if q.all():
logging.debug("deleting invalid entries from AccidentMarker")
q.delete(synchronize_session="fetch")
db.session.commit()


def delete_cbs_entries(start_year, batch_size):
"""
deletes all CBS markers (provider_code=1 or provider_code=3) in the database created in year and with provider code provider_code
Expand Down Expand Up @@ -1252,5 +1212,5 @@ def main(batch_size, source, load_start_year=None):
logging.debug("Finished Creating Hebrew DB Tables")
except Exception as ex:
print("Traceback: {0}".format(traceback.format_exc()))
raise CBSParsingFailed(message=str(ex))
raise RuntimeError(str(ex))
# Todo - send an email that an exception occured
46 changes: 0 additions & 46 deletions anyway/parsers/infographics_data_cache_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,52 +84,6 @@ def add_news_flash_to_cache(news_flash: NewsFlash):
return False


def get_infographics_data_from_cache(news_flash_id, years_ago) -> Dict:
db_item = (
db.session.query(InfographicsDataCache)
.filter(InfographicsDataCache.news_flash_id == news_flash_id)
.filter(InfographicsDataCache.years_ago == years_ago)
.first()
)
logging.debug(f"retrieved from cache {type(db_item)}:{db_item}"[:70])
db.session.commit()
try:
if db_item:
return json.loads(db_item.get_data())
else:
return {}
except Exception as e:
logging.error(
f"Exception while extracting data from returned cache item flash_id:{news_flash_id},years:{years_ago})"
f"returned value {type(db_item)}"
f":cause:{e.__cause__}, class:{e.__class__}"
)
return {}


def get_infographics_data_from_cache_by_road_segment(road_segment_id, years_ago) -> Dict:
db_item = (
db.session.query(InfographicsRoadSegmentsDataCache)
.filter(InfographicsRoadSegmentsDataCache.road_segment_id == int(road_segment_id))
.filter(InfographicsRoadSegmentsDataCache.years_ago == int(years_ago))
.first()
)
logging.debug(f"retrieved from cache {type(db_item)}:{db_item}"[:70])
db.session.commit()
try:
if db_item:
return json.loads(db_item.get_data())
else:
return {}
except Exception as e:
logging.error(
f"Exception while extracting data from returned cache item flash_id:{road_segment_id},years:{years_ago})"
f"returned value {type(db_item)}"
f":cause:{e.__cause__}, class:{e.__class__}"
)
return {}


def get_cache_retrieval_query(params: RequestParams):
res = params.resolution
loc = params.location_info
Expand Down
55 changes: 0 additions & 55 deletions anyway/parsers/location_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,61 +258,6 @@ def set_accident_resolution(accident_row):
logging.info("bug in accident resolution")


def reverse_geocode_extract(latitude, longitude):
"""
this method takes a latitude, longitude and returns a dict of the corresponding
location found on google maps (by that string), describing details of the location found and the geometry
:param latitude: latitude
:param longitude: longitude
:return: a dict containing data about the found location on google maps, with the keys: street,
road_no [road number], intersection, city, address, district and the geometry of the location.
"""
street = None
road_no = None
intersection = None
subdistrict = None
city = None
district = None
try:
gmaps = googlemaps.Client(key=secrets.get("GOOGLE_MAPS_KEY"))
geocode_result = gmaps.reverse_geocode((latitude, longitude))

# if we got no results, move to next iteration of location string
if not geocode_result:
return None
except Exception as _:
logging.info("exception in gmaps")
return None
# logging.info(geocode_result)
response = geocode_result[0]
geom = response["geometry"]["location"]
for item in response["address_components"]:
if "route" in item["types"]:
if item["short_name"].isdigit():
road_no = int(item["short_name"])
else:
street = item["long_name"]
elif "point_of_interest" in item["types"] or "intersection" in item["types"]:
intersection = item["long_name"]
elif "locality" in item["types"]:
city = item["long_name"]
elif "administrative_area_level_2" in item["types"]:
subdistrict = item["long_name"]
elif "administrative_area_level_1" in item["types"]:
district = item["long_name"]
address = response["formatted_address"]
return {
"street": street,
"road_no": road_no,
"intersection": intersection,
"city": city,
"address": address,
"subdistrict": subdistrict,
"district": district,
"geom": geom,
}


def geocode_extract(location):
"""
this method takes a string representing location and a google maps key and returns a dict of the corresponding
Expand Down
24 changes: 0 additions & 24 deletions anyway/parsers/news_flash_db_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,6 @@ def execute(self, *args, **kwargs):
def commit(self, *args, **kwargs):
return self.db.session.commit(*args, **kwargs)

def recreate_table_for_location_extraction(self):
with self.db.session.begin():
self.db.session.execute("""TRUNCATE cbs_locations""")
self.db.session.execute("""INSERT INTO cbs_locations
(SELECT ROW_NUMBER() OVER (ORDER BY road1) as id, LOCATIONS.*
FROM
(SELECT DISTINCT road1,
road2,
non_urban_intersection_hebrew,
yishuv_name,
street1_hebrew,
street2_hebrew,
district_hebrew,
region_hebrew,
road_segment_name,
longitude,
latitude
FROM markers_hebrew
WHERE (provider_code=1
OR provider_code=3)
AND (longitude is not null
AND latitude is not null)) LOCATIONS)"""
)

def get_markers_for_location_extraction(self):
query_res = self.execute(
"""SELECT * FROM cbs_locations"""
Expand Down
6 changes: 3 additions & 3 deletions anyway/parsers/registered.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
from datetime import datetime
from anyway.models import RegisteredVehicle, DeprecatedCity
from anyway.utilities import time_delta, CsvReader, ImporterUI, truncate_tables, decode_hebrew
from anyway.utilities import time_delta, CsvReader, ImporterUI, truncate_tables
from anyway.app_and_db import db


Expand Down Expand Up @@ -88,9 +88,9 @@ def row_parse(self, row):

return {
"year": self._report_year,
"name": decode_hebrew(name),
"name": name,
"name_eng": row[COLUMN_CITY_NAME_ENG].strip(),
"search_name": decode_hebrew(search_name),
"search_name": search_name,
"motorcycle": self.as_int(row[COLUMN_CITY_TOTAL_MOTORCYCLE]),
"special": self.as_int(row[COLUMN_CITY_TOTAL_SPECIAL]),
"taxi": self.as_int(row[COLUMN_CITY_TOTAL_TAXI]),
Expand Down
Loading

0 comments on commit 4cdd88c

Please sign in to comment.