-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
147 lines (119 loc) · 4.53 KB
/
app.py
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
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
import requests
from rdflib import Graph, Literal, Namespace, RDF, URIRef
from rdflib.namespace import RDFS
# Function to parse the date string to datetime
def parse_date(date):
return pd.to_datetime(date)
# Function to plot the cumulative sum of Mass
def plot_cumulative_mass(df):
df['Date'] = df['Date / YYYY-MM-DD'].apply(parse_date)
df['Total Mass'] = df['Mass / kg'].cumsum()
# Create figure
fig = go.Figure()
# Add line trace
fig.add_trace(go.Scatter(x=df['Date'], y=df['Total Mass'], mode='lines', name='Total Mass'))
# Add filled area trace
fig.add_trace(go.Scatter(x=df['Date'], y=df['Total Mass'], fill='tozeroy', mode='none', fillcolor='rgba(0,100,80,0.2)', name=""))
# Update layout
fig.update_layout(title='Total Mass of Picked Litter', xaxis_title='Date', yaxis_title='Total Mass / kg')
st.plotly_chart(fig)
# Function to parse JSON-LD data into a graph
def parse_jsonld(jsonld_data):
graph = Graph()
graph.parse(data=jsonld_data, format='json-ld')
return graph
# Function to display location coordinates on a map
def display_map(graph):
# Define the SPARQL query to retrieve latitude and longitude
query = """
PREFIX schema: <http://schema.org/>
SELECT ?latitude ?longitude
WHERE {
?place a schema:Place .
?place schema:latitude ?latitude .
?place schema:longitude ?longitude .
}
"""
# Execute the query
results = graph.query(query)
# Process the results into a DataFrame
locations = []
for row in results:
latitude = float(row['latitude'])
longitude = float(row['longitude'])
locations.append({'latitude': latitude, 'longitude': longitude})
if locations:
df = pd.DataFrame(locations)
st.map(df)
else:
st.write("No locations found.")
def add_multiselect_options(graph):
# Define the SPARQL query to retrieve photograph labels
query = """
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label
WHERE {
?photo a schema:Photograph .
?photo schema:about ?subject .
?subject rdfs:label ?label .
}
"""
# Execute the query
results = graph.query(query)
# Extract the labels from the query results and remove duplicates
options = list(set(str(row['label']) for row in results))
# Use Streamlit multiselect to select photographs
selected_options = st.multiselect("Select Photographs", options)
return selected_options
# Function to retrieve and display selected images
def display_selected_images(graph, selected_options):
# Define the SPARQL query to retrieve photograph URLs based on selected options
query = """
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?photo ?url
WHERE {
?photo a schema:Photograph .
?photo schema:about ?subject .
?subject rdfs:label ?label .
?photo schema:url ?url .
FILTER(?label IN (%s))
}
""" % ', '.join(['"' + option + '"' for option in selected_options])
# Execute the query
results = graph.query(query)
# Display images for each selected photograph URL
for row in results:
st.image(str(row['url']))
# Streamlit app
def main():
st.title('Trondheim Trash Data Hub')
# Read data from CSV file
csv_url = 'https://raw.githubusercontent.com/jsimonclark/TrondheimTrash/main/data/MassData.csv'
df = pd.read_csv(csv_url)
# Read data from json-ld file
jsonld_url = 'https://raw.githubusercontent.com/jsimonclark/TrondheimTrash/main/data/TrondheimTrash.json'
# Plot cumulative sum of Mass
#st.subheader('Cumulative Sum of Mass')
plot_cumulative_mass(df)
# Retrieve JSON-LD data from the URL
response = requests.get(jsonld_url)
# Check if the request was successful
if response.status_code == 200:
# Parse JSON-LD data into an RDF graph
graph = parse_jsonld(response.text)
print("Data retrieved successfully and parsed into an RDF graph.")
else:
print("Failed to retrieve JSON-LD data. Status code:", response.status_code)
# Add multi-select options for photographs
selected_options = add_multiselect_options(graph)
# Display location coordinates on a map
st.subheader('Location Map')
display_map(graph)
display_selected_images(graph, selected_options)
if __name__ == "__main__":
main()