Spaces:
Runtime error
Runtime error
File size: 6,149 Bytes
17e77ea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 30 19:55:23 2022
@author: syed
"""
import streamlit as st
from PIL import Image
import base64
from streamlit_folium import folium_static
import folium
from utils import geoutil
from disambiguation import disambiguate
import spacy
import en_core_web_md
from streamlit.components.v1 import html
def nav_page(page_name, timeout_secs=3):
nav_script = """
<script type="text/javascript">
function attempt_nav_page(page_name, start_time, timeout_secs) {
var links = window.parent.document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (links[i].href.toLowerCase().endsWith("/" + page_name.toLowerCase())) {
links[i].click();
return;
}
}
var elasped = new Date() - start_time;
if (elasped < timeout_secs * 1000) {
setTimeout(attempt_nav_page, 100, page_name, start_time, timeout_secs);
} else {
alert("Unable to navigate to page '" + page_name + "' after " + timeout_secs + " second(s).");
}
}
window.addEventListener("load", function() {
attempt_nav_page("%s", new Date(), %d);
});
</script>
""" % (page_name, timeout_secs)
html(nav_script)
geojson = ""
def set_header(): # tetis Geospacy LOGO
LOGO_IMAGE = "title.jpg"
st.markdown(
"""
<style>
.container {
display: flex;
}
.logo-text {
font-weight:700 !important;
font-size:50px !important;
color: #52aee3 !important;
padding-left: 10px !important;
}
.logo-img {
float:right;
width: 10%;
height: 10%;
}
</style>
""",
unsafe_allow_html=True
)
st.markdown(
f"""
<div class="container">
<img class="logo-img" src="data:image/png;base64,{base64.b64encode(open(LOGO_IMAGE, "rb").read()).decode()}">
<p class="logo-text">SpatialParse</p>
</div>
""",
unsafe_allow_html=True
)
def set_selected_entities(doc, types):
gpe_selected=""
loc_selected=""
rse_selected=""
if "g" in types:
gpe_selected ="GPE"
if "l" in types:
gpe_selected ="LOC"
if "r" in types:
gpe_selected ="RSE"
ents = [ent for ent in doc.ents if ent.label_ == gpe_selected or ent.label_ == loc_selected or ent.label_ == rse_selected]
doc.ents = ents
return doc
def extract_spatial_entities(model,text, types):
# nlp = spacy.load(model)
nlp = spacy.load('en_core_web_md')
nlp.add_pipe("spatial_pipeline", after="ner")
doc = nlp(text)
doc = set_selected_entities(doc, types)
return doc
def view_polygon_menu():
global geojson
st.sidebar.markdown("## View")
#st.sidebar.markdown("Select the **format** to view coordinates")
view = st.sidebar.radio(
"Select the format to view coordinates",
('Map', 'GEOJson'), horizontal=True)
st.sidebar.markdown("## Compute")
is_midmid = st.sidebar.radio(
"Do you Want to compute polygons with mid or midmid point",
('midpoint', 'midmidpoint'), horizontal=True)
midmid = False
if is_midmid == "midmidpoint":
midmid = True
params = st.experimental_get_query_params()
ase = None
level_1= None
level_2= None
level_3= None
if "entity" in params:
ase, level_1, level_2, level_3 = geoutil.get_ent(params["entity"][0])
md = "<span><b>ASE:</b> "+ str(ase)+" <b>Level 1:</b> "+ str(level_1)+" <b>Level 2:</b> "+ str(level_2)+" <b>Level 3:</b> "+ str(level_3)+"</span>"
st.write(md, unsafe_allow_html=True)
if view == "Map":
if "text" in params:
print(params["model"], 'mmmm')
doc = extract_spatial_entities(params["model"][0],params["text"][0],params["type"][0])
geojson = disambiguate.dismabiguate_entities(doc, params["entity"][0],ase, level_1, level_2, level_3, midmid)
draw_location(geojson)
elif view == "GEOJson":
if "text" in params:
doc = extract_spatial_entities(params["model"][0],params["text"][0],params["type"][0])
geojson = disambiguate.dismabiguate_entities(doc, params["entity"][0],ase, level_1, level_2, level_3, midmid)
displayGeoJson(geojson)
def set_geojson_menu():
global geojson
st.sidebar.markdown("## Export")
st.sidebar.markdown("Do you want to Export **GEOJson**?")
export = st.sidebar.button("Export")
if(export):
params = st.experimental_get_query_params()
disambiguate.export(params["entity"][0],geojson)
def set_map_menu():
st.sidebar.markdown("## Rate Polygon")
#st.sidebar.markdown("Do you want to rate the **Polygon**?")
rate = st.sidebar.radio(
"Do you want to rate the Polygon?",
('Yes', 'No'), horizontal=True)
rate_btn = st.sidebar.button("Rate")
if rate_btn:
if rate == "Yes":
nav_page("Rate")
def draw_location(geojson):
#gj = json.load(geojson)
centroid = geojson['features'][0]['properties']['centroid']
centroid = (centroid[0],centroid[1])
my_map = folium.Map(location=[centroid[1], centroid[0]],
zoom_start =12)
folium.GeoJson(geojson,
smooth_factor=1,
style_function = lambda x: {
'fillColor': 'green',
'color': 'blue',
'weight': 1.5,
'fillOpacity': 0.3
},
popup =True,
name= geojson['features'][0]['id']).add_to(my_map)
folium_static(my_map)
def displayGeoJson(geojson):
#path = ent._.rse_id+".geojson"
#with open(path) as f:
#gj = json.load(geojson)
st.json(geojson)
set_header()
view_polygon_menu()
set_map_menu()
set_geojson_menu()
#displayGeoJson()
|