From 9feaf0be3ffc6c7a68beb87f0696f9e106323aa0 Mon Sep 17 00:00:00 2001 From: alvaro Date: Sun, 12 Feb 2023 21:27:28 -0300 Subject: [PATCH] Upload files to '' Added parser and plotter files --- plotter.py | 45 +++++++++++++++++++++++++++++++ smn_parser.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 plotter.py create mode 100644 smn_parser.py diff --git a/plotter.py b/plotter.py new file mode 100644 index 0000000..60bd8ca --- /dev/null +++ b/plotter.py @@ -0,0 +1,45 @@ +import requests +import geopandas as gpd +#import folium +import matplotlib.pyplot as plt +from capparselib.parsers import CAPParser + +from xml.etree import ElementTree + +from shapely.geometry import Point, Polygon + +import contextily as ctx + + +linktext = requests.get('http://forms.smn.gob.ar/feeds/CAP/oladecalor/87765_cap_en.xml') +alert_list = CAPParser(linktext.text).as_dict() +polyList = list() + +if 'cap_area' in alert_list[0]['cap_info'][0]: + for area in alert_list[0]['cap_info'][0]['cap_area']: + for polygon_str in area['polygons']: + polygon_str_tokens = str(polygon_str).split(' ') + points = list() + for token in polygon_str_tokens: + coords_str = token.split(',') + if coords_str[0] != '': + x_coord = float(coords_str[1]) + y_coord = float(coords_str[0]) + points.append(Point(x_coord, y_coord)) + + polyList.append(Polygon(points)) + +crs = {'init': 'epsg:4326'} + + + +for poly in polyList: + geo_poly = gpd.GeoSeries(poly, crs=crs) + geo_poly.crs = 'epsg:4326' + + area_gdf = gpd.GeoDataFrame(geometry=geo_poly) + ax = area_gdf.plot(alpha=0.5) + ctx.add_basemap(ax, zoom=10, crs='EPSG:4326',source=ctx.providers.CartoDB.Positron) + #ax.set_axis_off() + +plt.show() diff --git a/smn_parser.py b/smn_parser.py new file mode 100644 index 0000000..9766e07 --- /dev/null +++ b/smn_parser.py @@ -0,0 +1,74 @@ + +import requests +from capparselib.parsers import CAPParser + +from xml.etree import ElementTree + +from shapely.geometry import Point, Polygon + +# Create Point objects +p1 = Point(-71.32,-41.15) + + +# Create a Polygon +#coords = [(24.950899, 60.169158), (24.953492, 60.169158), (24.953510, 60.170104), (24.950958, 60.169990)] +#poly = Polygon(coords) + + +response = requests.get('https://ssl.smn.gob.ar/CAP/AR.php') +response.encoding = 'UTF-8' +# create element tree object +root = ElementTree.fromstring(response.text) + +# get root element +#root = tree.getroot() + +# create empty list for news items +newsitems = [] + +# iterate news items +for item in root.findall('./channel/item'): + # empty news dictionary + news = {} + + + for child in item: + + # special checking for namespace object content:media + #if child.tag == 'link' or child.tag == 'description': + #or child.tag == 'title' or child.tag == 'description': + + #print(child.tag) + #print(child.text) + + if child.tag == 'link': + linktext = requests.get(child.text) + alert_list = CAPParser(linktext.text).as_dict() + for alert in alert_list: + for info in alert['cap_info']: + if 'cap_area' in info: + for area in info['cap_area']: + for polygon_str in area['polygons']: + + polygon_str_tokens = str(polygon_str).split(' ') + points = list() + for token in polygon_str_tokens: + coords_str = token.split(',') + if coords_str[0] != '': + x_coord = float(coords_str[1]) + y_coord = float(coords_str[0]) + points.append(Point(x_coord, y_coord)) + + poly = Polygon(points) + #print(poly) + + if p1.within(poly): + print(child.text) + print(info['cap_headline']) + print(alert['cap_sent']) + print(info['cap_expires']) + print(info['cap_description']) + + + #print(alert_list) +