def predictedDestination(iesGraph, vesselMMSI, destinations, assessmentDT): vessel = ies.createIdentifiedEntity(iesGraph, ies.vessel, URIRef(dataUri + "_vessel_mmsi_" + vesselMMSI), idClass=ies.commsIdentifier, namingScheme=ies.mmsiNs) for destination in destinations: portName = destination[0] un_locode = destination[1] iso3166ThreeLetterCountryCode = destination[2] probability = destination[3] if un_locode != "": #If there's a locode for the Port, then let's make sure we have a consistent URI for it too ! portUri = URIRef("http://www.un.org/locode#" + un_locode.replace(" ", "")) else: portUri = None port = ies.instantiate(iesGraph, ies.port, portUri) ies.addName(iesGraph, port, portName, ies.placeName) if un_locode != "": ies.addIdentifier(iesGraph, port, un_locode, idClass=ies.unLocode) if iso3166ThreeLetterCountryCode != "": #If the country has been specified, then create an appropriate URI for the country country = ies.instantiate( iesGraph, ies.country, URIRef("http://iso.org/iso3166-alpha3#" + iso3166ThreeLetterCountryCode)) ies.addIdentifier( iesGraph, country, iso3166ThreeLetterCountryCode, idClass=ies.iso3166A3, idUri=URIRef("http://iso.org/iso3166-alpha3#" + iso3166ThreeLetterCountryCode + "_code")) ies.addToGraph(iesGraph, port, ies.ipao, country) sailing = ies.instantiate(iesGraph, ies.sailing) vu = ies.instantiate(iesGraph, ies.vehicleUsed) ies.addToGraph(iesGraph, vu, ies.ipo, vessel) ies.addToGraph(iesGraph, vu, ies.ipi, sailing) arrival = ies.instantiate(iesGraph, ies.arrival) ies.addToGraph(iesGraph, arrival, ies.ieo, sailing) ies.addToGraph(iesGraph, arrival, ies.il, port) ies.assessPwProbability( iesGraph, [sailing, vu, arrival], TrackAnalytics, probability, True, assessmentDT) #Add the probability of the coopering
def cooperingAtSea(iesGraph, startTime, endTime, vesselMmsiList, probability, assessmentDT, usePHIA, vesselStartTimeList=[], vesselEndTimeList=[]): vessels = [] cooperEvent = ies.instantiate(iesGraph, _class=ies.cooper) if startTime != '': ies.startsIn(iesGraph, cooperEvent, startTime) if endTime != '': ies.endsIn(iesGraph, cooperEvent, endTime) pwItems = [cooperEvent] #things that need to be in a possible world for i, mmsi in enumerate(vesselMmsiList): vessel = ies.createIdentifiedEntity(iesGraph, ies.vessel, URIRef(dataUri + "_vessel_mmsi_" + mmsi), idClass=ies.commsIdentifier, namingScheme=ies.mmsiNs) coopering = ies.instantiate(iesGraph, _class=ies.coopering) pwItems.append(coopering) ies.addToGraph(iesGraph, coopering, ies.ipi, cooperEvent) ies.addToGraph(iesGraph, coopering, ies.ipo, vessel) if len(vesselStartTimeList) == len( vesselMmsiList) and len(vesselStartTimeList) > i: vst = vesselStartTimeList[i] if vst != None and vst != '': ies.startsIn(iesGraph, coopering, vst) if len(vesselEndTimeList) == len( vesselMmsiList) and len(vesselEndTimeList) > i: vet = vesselEndTimeList[i] if vet != None and vet != '': ies.endsIn(iesGraph, coopering, vet) ies.assessPwProbability( iesGraph, pwItems, TrackAnalytics, probability=probability, usePHIA=usePHIA, assessDate=assessmentDT) #Add the probability of the coopering return cooperEvent
def following(iesGraph, followerMMSI, followedMMSI, startTimeStamp, endTimeStamp, inferenceSystem): #Create the follow event object fol = ies.instantiate(iesGraph=iesGraph, _class=ies.follow) #Now put the start and end on the following event... ies.startsIn(iesGraph=iesGraph, item=fol, timeString=startTimeStamp) ies.endsIn(iesGraph=iesGraph, item=fol, timeString=endTimeStamp) #create this follower and followed transponders - these may already be i the graph, but this should get caught followerURI = ies.createLocationTransponder(iesGraph=iesGraph, mmsi=followerMMSI) followedURI = ies.createLocationTransponder(iesGraph=iesGraph, mmsi=followedMMSI) #Now create the participations of the follower and followed folrPart = ies.instantiate(iesGraph=iesGraph, _class=ies.follower) ies.addToGraph(iesGraph=iesGraph, subject=folrPart, predicate=ies.ipi, obj=fol) ies.addToGraph(iesGraph=iesGraph, subject=folrPart, predicate=ies.ipo, obj=followerURI) foldPart = ies.instantiate(iesGraph=iesGraph, _class=ies.followed) ies.addToGraph(iesGraph=iesGraph, subject=foldPart, predicate=ies.ipi, obj=fol) ies.addToGraph(iesGraph=iesGraph, subject=foldPart, predicate=ies.ipo, obj=followedURI) if (inferenceSystem): pw = ies.instantiate(iesGraph=iesGraph, _class=ies.possibleWorld) ies.addToGraph(iesGraph=iesGraph, subject=fol, predicate=ies.ipao, obj=pw) assessment = ies.instantiate(iesGraph=iesGraph, _class=ies.assess) ies.addToGraph(iesGraph=iesGraph, subject=assessment, predicate=ies.ass, obj=pw) assr = ies.instantiate(iesGraph=iesGraph, _class=ies.assessor) ies.addToGraph(iesGraph=iesGraph, subject=assr, predicate=ies.ipi, obj=assessment) ies.addToGraph(iesGraph=iesGraph, subject=assr, predicate=ies.ipo, obj=inferenceSystem) #Now put the assessment in a period - i.e. when the assessment was done ies.putInPeriod(iesGraph=iesGraph, item=assessment, timeString="2007-01-02T09:17:04")
def createLocationObservation(iesGraph, ping, obs=None, transponder=None, measures=None): mmsi = str(ping[0]) timestamp = str(ping[1]) lat = float(ping[2]) lon = float(ping[3]) print(lat, lon) if transponder == None: transponder = ies.createLocationTransponder(iesGraph=iesGraph, mmsi=mmsi) lo = ies.instantiate(iesGraph=iesGraph, _class=ies.locationObservation) #If track emulation is not required, obs will be None. If it's not None, make the LocationObservation (lo) part of the overall track observation if obs: ies.addToGraph(iesGraph=iesGraph, subject=lo, predicate=ies.ipao, obj=obs) #...and the ParticularPeriod in which the observation occurred ies.putInPeriod(iesGraph=iesGraph, item=lo, timeString=timestamp) #And involve the transponder in that location observation ltPart = ies.instantiate(iesGraph=iesGraph, _class=ies.observedTarget) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.ipo, obj=transponder) #participation of the transponder ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.ipi, obj=lo) #participation in the LocationObservation #Now the observed location, a geopoint with a lat and long - using a geohash to give each point a unique uri gp = URIRef(ies.dataUri + "latlong" + str(lat) + "," + str(lon)) ies.instantiate(iesGraph=iesGraph, _class=ies.geoPoint, instance=gp) #Add the lat and long values as identifiers of the geopoint...firstly creating repeatable URIs for them so they don't overwrite latObj = URIRef(gp.toPython() + "_lat") lonObj = URIRef(gp.toPython() + "_lon") ies.instantiate(iesGraph=iesGraph, _class=ies.latitude, instance=latObj) ies.instantiate(iesGraph=iesGraph, _class=ies.longitude, instance=lonObj) ies.addToGraph(iesGraph=iesGraph, subject=gp, predicate=ies.iib, obj=latObj) ies.addToGraph(iesGraph=iesGraph, subject=gp, predicate=ies.iib, obj=lonObj) #Add the representation values to the lat and lon objects ies.addToGraph(iesGraph=iesGraph, subject=latObj, predicate=ies.rv, obj=Literal(lat, datatype=XSD.string)) ies.addToGraph(iesGraph=iesGraph, subject=lonObj, predicate=ies.rv, obj=Literal(lon, datatype=XSD.string)) #Now the participation of the GeoPoint in the Observation gpPart = ies.instantiate(iesGraph=iesGraph, _class=ies.observedLocation) ies.addToGraph(iesGraph=iesGraph, subject=gpPart, predicate=ies.ipo, obj=gp) #participation of the GeoPoint ies.addToGraph(iesGraph=iesGraph, subject=gpPart, predicate=ies.ipi, obj=lo) #participation in the LocationObservation #This code fires if the measure classes etc. have been provided - it takes the speed and course over ground and processes that. if measures: sogVal = float(ping[4]) cogVal = float(ping[5]) sog = ies.addMeasure(iesGraph=iesGraph, measureClass=measures["sogClass"], value=sogVal, uom=measures["knots"]) cog = ies.addMeasure(iesGraph=iesGraph, measureClass=measures["cogClass"], value=cogVal, uom=measures["degTN"]) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.och, obj=sog) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.och, obj=cog)
def exportTrack(iesGraph, track, output="file", epsgCode="4326"): #The track dictionary should already have min and max timestamps for the pings it contains ies.initialiseGraph(iesGraph=iesGraph) #Add a parent observation obs = ies.instantiate(iesGraph=iesGraph, _class=ies.observation) ies.startsIn(iesGraph=iesGraph, item=obs, timeString=track["minDateTime"].isoformat()) ies.endsIn(iesGraph=iesGraph, item=obs, timeString=track["maxDateTime"].isoformat()) measures = dict({}) #Now add the measure classes for Speed Over Ground and Course Over Ground...and knots for the Unit Of Measure measures["sogClass"] = ies.instantiate(iesGraph=iesGraph, _class=ies.classOfMeasure, instance=URIRef(ies.dataUri + "SpeedOverGround")) measures["cogClass"] = ies.instantiate(iesGraph=iesGraph, _class=ies.classOfMeasure, instance=URIRef(ies.dataUri + "CourseOverGround")) measures["knots"] = ies.instantiate(iesGraph=iesGraph, _class=ies.unitOfMeasure, instance=URIRef(ies.dataUri + "Knots")) measures["degTN"] = ies.instantiate(iesGraph=iesGraph, _class=ies.unitOfMeasure, instance=URIRef(ies.dataUri + "DegreesTrueNorth")) ies.addName(iesGraph=iesGraph, item=measures["sogClass"], nameString="Speed Over Ground") ies.addName(iesGraph=iesGraph, item=measures["cogClass"], nameString="Course Over Ground") ies.addName(iesGraph=iesGraph, item=measures["knots"], nameString="knots") ies.addName(iesGraph=iesGraph, item=measures["degTN"], nameString="degrees true North") #add the location transponder - We don't know this is necessarily a vessel. All we know is that we have a LocationTransponder. lt = ies.createLocationTransponder(iesGraph=iesGraph, mmsi=track["id"]) obsvd = ies.instantiate(iesGraph=iesGraph, _class=ies.observedTarget) ies.addToGraph(iesGraph=iesGraph, subject=obsvd, predicate=ies.ipo, obj=lt) ies.addToGraph(iesGraph=iesGraph, subject=obsvd, predicate=ies.ipi, obj=obs) #now go through the individual location observations and add those... for ping in track["pings"]: createLocationObservation(iesGraph=iesGraph, ping=ping, transponder=lt, obs=obs, measures=measures) if output == "kafka": ies.sendToKafka(iesGraph=iesGraph, kProducer=kafkaBroker, kTopic=iesKafkaTopic) else: ies.saveRdf( iesGraph, './data/track' + track["id"] + '-' + str(track["counter"]) + '.ies.ttl')
def createParentObservation(iesGraph): return ies.instantiate(iesGraph=iesGraph, _class=ies.observation)
def exportStoneSoupTrack(iesGraph, trackId, states, mapping, targetId, targetIdType=ies.commsIdentifier, targetIdNamingScheme=ies.mmsiNs, epsgCode="4326"): minDT = None maxDT = None ies.setDataUri(dataUri) #Create the observation for the whole track obs = ies.instantiate(iesGraph=iesGraph, _class=ies.observation, instance=URIRef(dataUri + trackId)) #Create the transponder (we may already have this) if targetIdNamingScheme != ies.mmsiNs: transponderUri = URIRef(dataUri + "_transponderID_" + targetId) else: transponderUri = URIRef(dataUri + "_MMSI_" + targetId) transponder = ies.createIdentifiedEntity( iesGraph=iesGraph, entityClass=ies.locationTransponder, entityID=targetId, namingScheme=targetIdNamingScheme, uri=transponderUri) for state in states: #need to get max and min timestamp for each state so we can establish start and end of the track if minDT == None: minDT = state.timestamp elif state.timestamp < minDT: minDT = state.timestamp if maxDT == None: maxDT = state.timestamp elif state.timestamp > maxDT: maxDT = state.timestamp #Create the Location observation lo = ies.instantiate(iesGraph=iesGraph, _class=ies.locationObservation) if obs: ies.addToGraph(iesGraph=iesGraph, subject=lo, predicate=ies.ipao, obj=obs) #...and the ParticularPeriod in which the observation occurred ies.putInPeriod(iesGraph=iesGraph, item=lo, timeString=str(state.timestamp)) ltPart = ies.instantiate(iesGraph=iesGraph, _class=ies.observedTarget) ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.ipo, obj=transponder) #participation of the transponder ies.addToGraph(iesGraph=iesGraph, subject=ltPart, predicate=ies.ipi, obj=lo) #participation in the LocationObservation #In this example, we have UTM coordinates, but Stone Soup can pretty much send you anything. So...we used EPSG codes ! gp = ies.instantiate(iesGraph=iesGraph, _class=ies.geoPoint) epRep = ies.instantiate(iesGraph=iesGraph, _class=ies.epsgRep) ies.addToGraph(iesGraph=iesGraph, subject=gp, predicate=ies.iib, obj=epRep) for i, measure in enumerate(state.state_vector[mapping, :]): epsgParam = ies.instantiate(iesGraph=iesGraph, _class=ies.epsgParams[i]) ies.addToGraph(iesGraph=iesGraph, subject=epsgParam, predicate=ies.ir, obj=epRep) ies.addToGraph(iesGraph=iesGraph, subject=epsgParam, predicate=ies.rv, obj=Literal(str(measure))) gpPart = ies.instantiate(iesGraph=iesGraph, _class=ies.observedLocation) ies.addToGraph(iesGraph=iesGraph, subject=gpPart, predicate=ies.ipo, obj=gp) #participation of the GeoPoint ies.addToGraph(iesGraph=iesGraph, subject=gpPart, predicate=ies.ipi, obj=lo) #participation in the LocationObservation #print(state.state_vector[mapping, :]) #print("-------------") ies.startsIn(iesGraph=iesGraph, item=obs, timeString=minDT.isoformat()) ies.endsIn(iesGraph=iesGraph, item=obs, timeString=maxDT.isoformat())
assr = ies.instantiate(iesGraph=iesGraph, _class=ies.assessor) ies.addToGraph(iesGraph=iesGraph, subject=assr, predicate=ies.ipi, obj=assessment) ies.addToGraph(iesGraph=iesGraph, subject=assr, predicate=ies.ipo, obj=inferenceSystem) #Now put the assessment in a period - i.e. when the assessment was done ies.putInPeriod(iesGraph=iesGraph, item=assessment, timeString="2007-01-02T09:17:04") #Set up the rdf graph graph = ies.initialiseGraph(None) #Now say one is following the other (they're not, but I didn't have any data where ships followed each other) #First we create an object for the system that detected it. hal = ies.instantiate(iesGraph=graph, _class=ies.system) ies.addName(iesGraph=graph, item=hal, nameString="HAL") #now create the following pattern, with our system included as the assessor following(iesGraph=graph, followerMMSI="367000150", followedMMSI="366952890", startTimeStamp="2007-01-01T00:00:09", endTimeStamp="2007-01-01T00:05:40", inferenceSystem=hal) ies.saveRdf(graph=graph, filename='following.ies.ttl')