Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import plotly.graph_objects as go | |
| import json | |
| from shapely.geometry import Point, Polygon | |
| with open('./dnp_bnd.geojson', 'r') as file: | |
| dnp_area = json.load(file) | |
| with open('./nrf_bnd.geojson', 'r') as file: | |
| nrf_area = json.load(file) | |
| def check_area(lat, lon, area): | |
| # Create a Point | |
| point = Point(lon, lat) | |
| # Function to check if point is in any polygon | |
| def is_point_in_any_polygon(point, geojson_data): | |
| for feature in geojson_data['features']: | |
| polygon = Polygon(feature['geometry']['coordinates'][0][0]) | |
| if point.within(polygon): | |
| return True | |
| return False | |
| # Check if the point is in the polygon | |
| return is_point_in_any_polygon(point, area) | |
| def filter_map(lat, lon): | |
| is_in_dnp = check_area(lat, lon, dnp_area) | |
| is_in_nrf = check_area(lat, lon, nrf_area) | |
| print(is_in_dnp) | |
| print(is_in_nrf) | |
| if is_in_dnp and not is_in_nrf: | |
| text = 'อยู่ในขอบเขตป่าตามกฎ EUDR' | |
| elif not is_in_dnp and is_in_nrf: | |
| text = 'อยู่ในขอบเขตป่าตามกฎหมายไทย' | |
| elif is_in_dnp and is_in_nrf: | |
| text = 'อยู่ในขอบเขตป่าตามกฎหมายไทยและกฎ EUDR' | |
| else: | |
| text = 'ไม่อยู่ในขอบเขตป่า' | |
| fig = go.Figure(go.Scattermapbox( | |
| # customdata=text_list, | |
| lat=[lat], | |
| lon=[lon], | |
| mode='markers', | |
| marker=go.scattermapbox.Marker( | |
| size=13 | |
| ), | |
| hoverinfo="text", | |
| hovertemplate=f'<b>ตำแหน่ง</b>: {lat},{lon}<br><b></b>สถานะตำแหน่ง: {text}' | |
| )) | |
| # fig.update_layout( | |
| # mapbox_style="open-street-map", | |
| # hovermode='closest', | |
| # mapbox=dict( | |
| # bearing=0, | |
| # center=go.layout.mapbox.Center( | |
| # lat=lat, | |
| # lon=lon | |
| # ), | |
| # pitch=0, | |
| # zoom=9 | |
| # ), | |
| # ) | |
| fig.update_layout( | |
| mapbox={ | |
| 'style':"open-street-map", | |
| 'center': go.layout.mapbox.Center( | |
| lat=lat, | |
| lon=lon | |
| ), | |
| 'zoom': 9, | |
| 'layers':[ | |
| {'sourcetype': 'geojson', | |
| 'source': dnp_area['features']}, | |
| ] | |
| }, | |
| ) | |
| return fig | |
| with gr.Blocks() as demo: | |
| with gr.Column(): | |
| with gr.Row(): | |
| lat = gr.Number(value=9.00, label="ละจิจูด") | |
| lon = gr.Number(value=99.076, label="ลองจิจูด") | |
| btn = gr.Button(value="ค้นหาตำแหน่ง") | |
| map = gr.Plot() | |
| demo.load(filter_map, [lat, lon], map) | |
| btn.click(filter_map, [lat, lon], map) | |
| if __name__ == "__main__": | |
| demo.launch() | |