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
df = pd.read_csv('Jasmin.csv')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 df = pd.read_csv('Jasmin.csv')

File C:\Python310\lib\site-packages\pandas\util\_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    305 if len(args) > num_allow_args:
    306     warnings.warn(
    307         msg.format(arguments=arguments),
    308         FutureWarning,
    309         stacklevel=stacklevel,
    310     )
--> 311 return func(*args, **kwargs)

File C:\Python310\lib\site-packages\pandas\io\parsers\readers.py:680, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
    665 kwds_defaults = _refine_defaults_read(
    666     dialect,
    667     delimiter,
   (...)
    676     defaults={"delimiter": ","},
    677 )
    678 kwds.update(kwds_defaults)
--> 680 return _read(filepath_or_buffer, kwds)

File C:\Python310\lib\site-packages\pandas\io\parsers\readers.py:575, in _read(filepath_or_buffer, kwds)
    572 _validate_names(kwds.get("names", None))
    574 # Create the parser.
--> 575 parser = TextFileReader(filepath_or_buffer, **kwds)
    577 if chunksize or iterator:
    578     return parser

File C:\Python310\lib\site-packages\pandas\io\parsers\readers.py:933, in TextFileReader.__init__(self, f, engine, **kwds)
    930     self.options["has_index_names"] = kwds["has_index_names"]
    932 self.handles: IOHandles | None = None
--> 933 self._engine = self._make_engine(f, self.engine)

File C:\Python310\lib\site-packages\pandas\io\parsers\readers.py:1217, in TextFileReader._make_engine(self, f, engine)
   1213     mode = "rb"
   1214 # error: No overload variant of "get_handle" matches argument types
   1215 # "Union[str, PathLike[str], ReadCsvBuffer[bytes], ReadCsvBuffer[str]]"
   1216 # , "str", "bool", "Any", "Any", "Any", "Any", "Any"
-> 1217 self.handles = get_handle(  # type: ignore[call-overload]
   1218     f,
   1219     mode,
   1220     encoding=self.options.get("encoding", None),
   1221     compression=self.options.get("compression", None),
   1222     memory_map=self.options.get("memory_map", False),
   1223     is_text=is_text,
   1224     errors=self.options.get("encoding_errors", "strict"),
   1225     storage_options=self.options.get("storage_options", None),
   1226 )
   1227 assert self.handles is not None
   1228 f = self.handles.handle

File C:\Python310\lib\site-packages\pandas\io\common.py:789, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    784 elif isinstance(handle, str):
    785     # Check whether the filename is to be opened in binary mode.
    786     # Binary mode does not support 'encoding' and 'newline'.
    787     if ioargs.encoding and "b" not in ioargs.mode:
    788         # Encoding
--> 789         handle = open(
    790             handle,
    791             ioargs.mode,
    792             encoding=ioargs.encoding,
    793             errors=errors,
    794             newline="",
    795         )
    796     else:
    797         # Binary mode
    798         handle = open(handle, ioargs.mode)

FileNotFoundError: [Errno 2] No such file or directory: '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