Skip to content

Commit

Permalink
Fix/remove shapely (#1723)
Browse files Browse the repository at this point in the history
* Remove shapely dependency

* Entries found from sector, not address

* Instructions to find sector name from your address manually

* Add province and update documents links

* remove shapley from manifest.json + reformat montral_ca

---------

Co-authored-by: 5ila5 <[email protected]>
  • Loading branch information
julienboriasse and 5ila5 authored Jan 26, 2024
1 parent 78289d7 commit 1638a98
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ Waste collection schedules in the following formats and countries are supported.
- [City of Peterborough, ON](/doc/ics/recollect.md) / peterborough.ca
- [City of Vancouver](/doc/ics/recollect.md) / vancouver.ca
- [London (ON)](/doc/source/recyclecoach_com.md) / london.ca
- [Montreal](/doc/source/montreal_ca.md) / montreal.ca/info-collectes
- [Montreal (QC)](/doc/source/montreal_ca.md) / montreal.ca/info-collectes
- [Ottawa, Canada](/doc/ics/recollect.md) / ottawa.ca
- [RM of Morris, MB](/doc/ics/recollect.md) / mwmenviro.ca
- [Strathcona County, ON](/doc/ics/recollect.md) / strathcona.ca
Expand Down Expand Up @@ -1031,7 +1031,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Gore, Invercargill & Southland](/doc/source/wastenet_org_nz.md) / wastenet.org.nz
- [Hamilton City Council](/doc/source/hcc_govt_nz.md) / fightthelandfill.co.nz
- [Horowhenua District Council](/doc/source/horowhenua_govt_nz.md) / horowhenua.govt.nz
- [Hutt City Council](/doc/source/toogoodtowaste.md) / toogoodtowaste.co.nz
- [Hutt City Council](/doc/source/toogoodtowaste_co_nz.md) / toogoodtowaste.co.nz
- [Waipa District Council](/doc/source/waipa_nz.md) / waipadc.govt.nz
- [Wellington City Council](/doc/source/wellington_govt_nz.md) / wellington.govt.nz
</details>
Expand Down
4 changes: 2 additions & 2 deletions custom_components/waste_collection_schedule/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"documentation": "https://github.com/mampfes/hacs_waste_collection_schedule#readme",
"integration_type": "hub",
"iot_class": "cloud_polling",
"requirements": ["icalendar", "recurring_ical_events", "icalevents", "beautifulsoup4", "lxml", "shapely"],
"version": "1.45.0"
"requirements": ["icalendar", "recurring_ical_events", "icalevents", "beautifulsoup4", "lxml"],
"version": "1.45.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
from datetime import datetime, timedelta

import requests
from shapely.geometry import Point, shape
from waste_collection_schedule import Collection # type: ignore[attr-defined]

# Currently, Montreal does not offer an iCal/Webcal subscription method.
# The GeoJSON file provides sector-specific details.
# The waste collection schedule is then interpreted from English natural language. Not every sector follows the same structure.
# This method is not highly reliable but serves as an acceptable workaround until a better solution is provided by the city.

TITLE = "Montreal"
TITLE = "Montreal (QC)"
DESCRIPTION = "Source script for montreal.ca/info-collectes"
URL = "https://montreal.ca/info-collectes"
TEST_CASES = {
"6280 Chambord": {"address": "6280 rue Chambord"},
"2358 Monsabre": {"address": "2358 rue Monsabre"},
"10785 Clark": {"address": "10785 rue Clark"},
"Lasalle": {"sector": "LSL4"},
"Mercier-Hochelaga": {"sector": "MHM_42-5_B"},
"Ahuntsic": {"sector": "AC-2"},
}

API_URL = [
Expand Down Expand Up @@ -86,8 +85,8 @@


class Source:
def __init__(self, address):
self._address = address
def __init__(self, sector):
self._sector = sector

def get_collections(self, collection_day, weeks, start_date):
collection_day = time.strptime(collection_day, "%A").tm_wday
Expand All @@ -103,30 +102,6 @@ def get_collections(self, collection_day, weeks, start_date):
next_dates.append(next_collect)
return next_dates

def get_latitude_longitude_point(self):
# Get latitude & longitude of address
url = "https://geocoder.cit.api.here.com/6.2/search.json"

params = {
"gen": "9",
"app_id": "pYZXmzEqjmR2DG66DRIr",
"app_code": "T-Z-VT6e6I7IXGuqBfF_vQ",
"country": "CAN",
"state": "QC",
"searchtext": self._address,
"bbox": "45.39,-73.37;45.72,-74.00",
}

r = requests.get(url, params=params)
r.raise_for_status()

lat_long = r.json()["Response"]["View"][0]["Result"][0]["Location"][
"DisplayPosition"
]

# construct point based on lon/lat returned by geocoder
return Point(lat_long["Longitude"], lat_long["Latitude"])

def parse_green(self, schedule_message):
SOURCE_TYPE = "Green"
days = []
Expand Down Expand Up @@ -251,7 +226,7 @@ def parse_recycling_food(self, schedule_message, source_type):
pass # Skip if the day is out of range for the month
return entries

def get_data_by_source(self, source_type, url, point):
def get_data_by_source(self, source_type, url):
# Get waste collection zone by longitude and latitude

r = requests.get(url)
Expand All @@ -260,11 +235,9 @@ def get_data_by_source(self, source_type, url, point):
schedule = r.json()
entries = []

# check each polygon to see if it contains the point
# check the information for the sector
for feature in schedule["features"]:
sector_shape = shape(feature["geometry"])

if not sector_shape.contains(point):
if feature["properties"]["SECTEUR"] != self._sector:
continue
schedule_message = feature["properties"]["MESSAGE_EN"]

Expand All @@ -287,12 +260,10 @@ def get_data_by_source(self, source_type, url, point):
return entries

def fetch(self):
point = self.get_latitude_longitude_point()

entries = []
for source in API_URL:
try:
entries += self.get_data_by_source(source["type"], source["url"], point)
entries += self.get_data_by_source(source["type"], source["url"])
except Exception:
# Probably because the natural language format does not match known formats.
LOGGER.warning(
Expand Down
17 changes: 14 additions & 3 deletions doc/source/montreal_ca.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ waste_collection_schedule:
sources:
- name: montreal_ca
args:
address: ADDRESS
sector: SECTOR
```
### Configuration Variables
**address**
**sector**
*(string) (required)*
**How do I find my sector?**
- Download on your computer a [Montreal GeoJSON file](https://donnees.montreal.ca/dataset/2df0fa28-7a7b-46c6-912f-93b215bd201e/resource/5f3fb372-64e8-45f2-a406-f1614930305c/download/collecte-des-ordures-menageres.geojson)
- Visit https://geojson.io/
- Click on *Open* and select the Montreal GeoJSON file
- Find your sector on the map
![Alt text](../../images/montreal_ca_helper.png)
## Example
```yaml
waste_collection_schedule:
sources:
- name: montreal_ca
args:
address: 2812, rue Monsabre
address: MHM_41-1
```
Binary file added images/montreal_ca_helper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion info.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ recurring-ical-events>=2.0.2
requests>=2.31.0
urllib3>=2.0.7
jinja2>=3.1.2
lxml>=4.9.4
shapely>=2.0.2
lxml>=4.9.4

0 comments on commit 1638a98

Please sign in to comment.