Elephant GPS Collar Tracking#
import pandas as pd #for handling csv and csv contents
from rdflib import Graph, Literal, RDF, URIRef, Namespace #basic RDF handling
from rdflib.namespace import FOAF , XSD, SSN, SOSA #most common namespaces
import urllib.parse #for parsing strings to URI's
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import plotly.express as px
import stardog
import json
import io
import os
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 1
----> 1 import pandas as pd #for handling csv and csv contents
2 from rdflib import Graph, Literal, RDF, URIRef, Namespace #basic RDF handling
3 from rdflib.namespace import FOAF , XSD, SSN, SOSA #most common namespaces
ModuleNotFoundError: No module named 'pandas'
df = pd.read_csv('Jasmin.csv')
df['Altitude']= pd.to_numeric(df['Altitude'],errors='coerce')
df['Altitude'].mean().round(6)
143.757143
df.head(10)
ID | LocalDate | LocalTime | GMTDate | GMTTime | lat | long | Temperature | Speed | Direction | Altitude | Cov | HDOP | Distance | Count | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1SAT32 | 2011-10-26 | 07:40:35 AM | 2011-10-25 | 11:40:35 PM | 5.437206 | 118.111301 | 24.5 | 0.24 | 0 | NaN | 0 | 1.64 | 0 | 1 |
1 | 2SAT32 | 2011-10-26 | 03:41:35 PM | 2011-10-26 | 07:41:35 AM | 5.433402 | 118.108162 | 29.0 | 0.76 | 0 | NaN | 0 | 1.74 | 548 | 2 |
2 | 3SAT32 | 2011-10-26 | 11:40:35 PM | 2011-10-26 | 03:40:35 PM | 5.433734 | 118.108599 | 27.0 | 0.86 | 0 | NaN | 0 | 2.18 | 61 | 3 |
3 | 4SAT32 | 2011-10-27 | 11:40:34 PM | 2011-10-27 | 03:40:34 PM | 5.455738 | 118.183640 | 28.0 | 0.16 | 0 | NaN | 0 | 1.89 | 8669 | 4 |
4 | 5SAT32 | 2011-10-28 | 07:42:53 AM | 2011-10-27 | 11:42:53 PM | 5.456503 | 118.187684 | 27.5 | 1.46 | 0 | NaN | 1 | 2.50 | 456 | 5 |
5 | 6SAT32 | 2011-10-28 | 03:41:05 PM | 2011-10-28 | 07:41:05 AM | 5.483344 | 118.232541 | 33.5 | 0.06 | 0 | NaN | 5 | 1.45 | 5800 | 6 |
6 | 7SAT32 | 2011-10-28 | 03:41:05 PM | 2011-10-28 | 07:41:05 AM | 5.483344 | 118.232541 | 33.5 | 0.06 | 0 | NaN | 5 | 1.45 | 0 | 7 |
7 | 8SAT32 | 2011-10-28 | 03:41:05 PM | 2011-10-28 | 07:41:05 AM | 5.483344 | 118.232541 | 33.5 | 0.06 | 0 | NaN | 5 | 1.45 | 0 | 8 |
8 | 9SAT32 | 2011-10-28 | 07:40:34 PM | 2011-10-28 | 11:40:34 AM | 5.479701 | 118.243606 | 29.0 | 0.14 | 0 | NaN | 4 | 0.95 | 1291 | 9 |
9 | 10SAT32 | 2011-10-28 | 07:40:34 PM | 2011-10-28 | 11:40:34 AM | 5.479701 | 118.243606 | 29.0 | 0.14 | 0 | NaN | 5 | 0.95 | 0 | 10 |
df.describe()
lat | long | Temperature | Speed | Direction | Altitude | Cov | HDOP | Distance | Count | |
---|---|---|---|---|---|---|---|---|---|---|
count | 899.000000 | 899.000000 | 899.000000 | 899.000000 | 899.000000 | 7.000000 | 899.000000 | 899.000000 | 899.000000 | 899.000000 |
mean | 5.509015 | 118.237317 | 28.888765 | 0.587164 | 1.329255 | 143.757143 | 2.497219 | 2.550044 | 640.407119 | 450.000000 |
std | 0.073845 | 0.105518 | 4.016236 | 0.673300 | 17.429289 | 88.443143 | 2.170552 | 3.812480 | 917.534269 | 259.663243 |
min | 5.392483 | 117.981100 | -35.500000 | 0.000000 | 0.000000 | 101.500000 | 0.000000 | 0.710000 | 0.000000 | 1.000000 |
25% | 5.465016 | 118.199810 | 28.000000 | 0.180000 | 0.000000 | 103.050000 | 0.000000 | 1.295000 | 145.500000 | 225.500000 |
50% | 5.490733 | 118.248029 | 28.500000 | 0.370000 | 0.000000 | 114.900000 | 2.000000 | 1.830000 | 341.000000 | 450.000000 |
75% | 5.546202 | 118.311595 | 30.000000 | 0.735000 | 0.000000 | 120.200000 | 5.000000 | 2.715000 | 761.000000 | 674.500000 |
max | 5.706847 | 118.403456 | 37.500000 | 6.610000 | 343.000000 | 343.400000 | 5.000000 | 99.900000 | 9603.000000 | 899.000000 |
# 'Visualising Elephant Jasmin Movements'
# Creating and visualizing a scatter plot on Mapbox
fig = px.scatter_mapbox(df, lat='lat', lon='long',
animation_frame = 'GMTDate', animation_group = 'GMTTime',
color='Speed', size='Speed',
color_continuous_scale=px.colors.cyclical.IceFire,
size_max=50, zoom=1.75, hover_name='ID',
hover_data = ['GMTDate', 'GMTTime', 'lat','long'],
title = 'Visualising Elephant Jasmin Movements')
fig.update_layout(
height=800,
margin={'l': 0, 't': 0, 'b': 0, 'r': 0},
mapbox={
'center': {'lon': 118, 'lat': 5},
'accesstoken':"pk.eyJ1IjoibmFlaW1hIiwiYSI6ImNsNDRoa295ZDAzMmkza21tdnJrNWRqNmwifQ.-cUTmhr1Q03qUXJfQoIKGQ",
'style': 'satellite-streets',
#'style': "open-street-map",
#'style': 'outdoors',
'center': {'lon': 118, 'lat': 5},
'zoom': 8})
fig.show()
g = Graph()
ID = Namespace('DGFC_')
SOSA = Namespace('http://www.w3.org/ns/sosa/')
lat = Namespace('http://www.w3.org/2003/01/geo/wgs84_pos#')
long =Namespace('http://www.w3.org/2003/01/geo/wgs84_pos#')
alt = Namespace('http://www.w3.org/2003/01/geo/wgs84_pos#')
UNIT= Namespace('http://qudt.org/vocab/unit')
schema = Namespace('http://schema.org/')
uri=URIRef('http://www.w3.org/2000/01/rdf-schema#')
OBSPRO= Namespace('http://www.w3.org/ns/sosa/ObservableProperty/')
TIME = Namespace('http://www.w3.org/2006/time#')
VOID = Namespace('http://rdfs.org/ns/void#')
XMLNS = Namespace('http://www.w3.org/XML/1998/namespace')
for index, row in df.iterrows():
g.add((URIRef(ID+row['ID']), RDF.type, SOSA.Observation))
g.add((URIRef(ID+row['ID']), SOSA.Observation, Literal(row['ID'], datatype=XSD.string)))
g.add((URIRef(ID+row['ID']), URIRef(schema+'DGFC/elephant#Jasmin'), Literal(row['ID'], datatype=XSD.string) ))
g.add((URIRef(ID+row['ID']), TIME.localDate, Literal(row['LocalDate'], datatype=XSD.date)))
g.add((URIRef(ID+row['ID']), TIME.localTime, Literal(row['LocalTime'], datatype=XSD.time)))
g.add((URIRef(ID+row['ID']), TIME.gMTDate, Literal(row['GMTDate'], datatype=XSD.date)))
g.add((URIRef(ID+row['ID']), TIME.gMTTime, Literal(row['GMTTime'], datatype=XSD.time)))
g.add((URIRef(ID+row['ID']), lat.lat, Literal(row['lat'], datatype=XSD.float)))
g.add((URIRef(ID+row['ID']), long.long, Literal(row['long'], datatype=XSD.float)))
g.add((URIRef(ID+row['ID']), OBSPRO.Temperature, Literal(row['Temperature'], datatype=XSD.double)))
g.add((URIRef(ID+row['ID']), OBSPRO.Speed, Literal(row['Speed'], datatype=XSD.float)))
g.add((URIRef(ID+row['ID']), alt.alt, Literal(row['Altitude'], datatype=XSD.float)))
g.add((URIRef(ID+row['ID']), OBSPRO.Direction, Literal(row['Direction'], datatype=XSD.float)))
g.add((URIRef(ID+row['ID']), OBSPRO.Distance, Literal(row['Distance'], datatype=XSD.float)))
g.add((URIRef(ID+row['ID']), OBSPRO.HDOP, Literal(row['HDOP'], datatype=XSD.integer) ))
g.add((URIRef(ID+row['ID']), OBSPRO.Cov, Literal(row['Cov'], datatype=XSD.integer) ))
g.add((URIRef(ID+row['ID']), OBSPRO.Count, Literal(row['Count'], datatype=XSD.integer) ))
# print(g.serialize(format='turtle')).head(10)
# saving ontology to disk
g.serialize("Jasmin.rdf", format="ttl")
# adding serialized data to Stardog (knowledge graph platform)
conn_details = {
'endpoint': 'http://localhost:5820',
'username': 'admin',
'password': 'admin'
}
with stardog.Admin(**conn_details) as admin:
Jasmin = admin.new_database('Jasmin')
conn = stardog.Connection('Jasmin', **conn_details)
conn.begin()
conn.add(
stardog.content.File('Jasmin.rdf', stardog.content_types.TURTLE),
)
conn.commit()
# Fetch data back from Stardog
conn_details = {
'endpoint': 'http://localhost:5820',
'username': 'admin',
'password': 'admin'}
conn = stardog.Connection('FOO', **conn_details)
Jasmin= '''select * {GRAPH <urn:Jasmin> {?Jasmin <http://www.w3.org/2006/time#gMTDate> ?gMTDate;
<http://www.w3.org/2003/01/geo/wgs84_pos#long> ?Jasminlong;
<http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?Jasminlat;
<http://www.w3.org/ns/sosa/ObservableProperty/Speed> ?JasminSpeed;
<http://www.w3.org/ns/sosa/ObservableProperty/Temperature> ?JasminTemperature;
<http://www.w3.org/ns/sosa/ObservableProperty/Direction> ?JasminDirection;
FILTER(?gMTDate >= "2011-02-07"^^xsd:date && ?gMTDate <= "2012-02-15"^^xsd:date)}}'''
csv_resultsJasmin = conn.select(Jasmin, content_type='text/csv')
df = pd.read_csv(io.BytesIO(csv_resultsJasmin))
df.head(10)
Jasmin | gMTDate | Jasminlong | Jasminlat | JasminSpeed | JasminTemperature | JasminDirection | |
---|---|---|---|---|---|---|---|
0 | http://api.stardog.com/DGFC_100SAT32 | 2011-11-13 | 118.39331 | 5.676665 | 0.24 | 33.0 | 0.0 |
1 | http://api.stardog.com/DGFC_101SAT32 | 2011-11-13 | 118.39078 | 5.679592 | 0.33 | 28.5 | 0.0 |
2 | http://api.stardog.com/DGFC_102SAT32 | 2011-11-13 | 118.39047 | 5.681083 | 0.03 | 27.5 | 0.0 |
3 | http://api.stardog.com/DGFC_103SAT32 | 2011-11-13 | 118.39450 | 5.683758 | 0.72 | 27.5 | 0.0 |
4 | http://api.stardog.com/DGFC_104SAT32 | 2011-11-13 | 118.39483 | 5.684847 | 0.16 | 28.0 | 0.0 |
5 | http://api.stardog.com/DGFC_105SAT32 | 2011-11-14 | 118.39486 | 5.685885 | 0.62 | 30.0 | 0.0 |
6 | http://api.stardog.com/DGFC_106SAT32 | 2011-11-14 | 118.39564 | 5.685156 | 0.02 | 32.0 | 0.0 |
7 | http://api.stardog.com/DGFC_107SAT32 | 2011-11-14 | 118.37855 | 5.685184 | 0.42 | 29.0 | 0.0 |
8 | http://api.stardog.com/DGFC_108SAT32 | 2011-11-14 | 118.38297 | 5.683757 | 0.82 | 28.5 | 0.0 |
9 | http://api.stardog.com/DGFC_109SAT32 | 2011-11-14 | 118.38423 | 5.683193 | 0.52 | 28.0 | 0.0 |