Online Mapping Urban Temperature with Istsos and Python - Tutorial

Geospatial mapping of environmental variables is key for understanding habitats and in this case urban habitats. We have done research in temperature mapping stored in the cloud with Raspberrys and a Istsos server, this time we have coupled a GPS sensor to make an urban temperature map from the data retrieved from the server. This tutorial shows the procedure to retrieve the data, convert it to a geopandas dataframe, export as ESRI shapefile and understand the temperature distribution relation with the land cover.

Know more to:

Online sensor setup on a Raspberry Pi under the Istsos framework (SOS Standard)

Record GPS position on a Postgresql database using a Raspberry Pi

@hatarilabs

One example application of urban temperature mapping with Raspberry to a Istsos cluod and retrieved with Python

♬ sonido original - hatarilabs

Tutorial

Input files

You can download the input files from this link.

Code

#import required packages
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import matplotlib.pyplot as plt
import requests, json, os, math
from dotenv import load_dotenv
import pandas as pd
import geopandas as gpd
import folium

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

#load credentials
load_dotenv('envViewer.txt')
user = os.getenv('ISTSOSUSER')
password = os.getenv('ISTSOSPASSWORD')

auth = HTTPBasicAuth(user, password)

#set istsos urls
url = 'https://apps.hatarilabs.com/istsos'
service = 'temperature'
procedure = 'genericGeoSensor'
off = 'tempnetwork'

#define dates
beginTime = "2023-06-15T00:00:00-05:00"
endTime = "2023-06-16T20:00:00-05:00"

#get observations
observedProperties = requests.get(
    '%s/wa/istsos/services/%s/operations/getobservation/offerings/'
    '%s/procedures/%s/observedproperties/:/eventtime/%s/%s' % (
        url, service, off, procedure,beginTime,endTime),
    params={
        "qualityIndex": "False"
    }, auth=auth)

#print observed values as list
observedList = observedProperties.json()['data'][0]['result']['DataArray']['values']
print(observedList[-1])

#create dataframe
observedDf = pd.DataFrame(columns=['Date','Lon','Lat','Elev','Temp'])

observedDf['Date'] = pd.to_datetime([i[0] for i in observedList])
observedDf['Lon'] = [i[1] for i in observedList]
observedDf['Lat'] = [i[2] for i in observedList]
observedDf['Elev'] = [i[3] for i in observedList]
observedDf['Temp'] = [i[4] for i in observedList]
observedDf = observedDf.set_index('Date')

#create geodataframe
geometry = gpd.points_from_xy(observedDf.Lon, observedDf.Lat)
obsGeoDf = gpd.GeoDataFrame(
    observedDf[['Lon','Lat','Elev','Temp']], geometry=geometry
)

#for online map
meanLat = obsGeoDf.Lat.mean()
meanLon = obsGeoDf.Lon.mean()
map = folium.Map(location=[meanLat, meanLon], zoom_start=17)

# Create a geometry list from the GeoDataFrame
geoDfList = [[point.xy[1][0], point.xy[0][0]] for point in obsGeoDf.geometry]
i = 0
for index,coordinates in enumerate(geoDfList):
    partialTemp = obsGeoDf.iloc[index].Temp
    tempDelta = 10**(partialTemp - obsGeoDf.Temp.mean())
    map.add_child(folium.Circle(location=coordinates,radius=tempDelta,popup=partialTemp,icon=folium.Icon(color="%s" % "lightgray")))
map
['2023-06-15T10:20:58.387386-05:00', -76.992182, -12.114250666666667, 177.7, 23.6875]
Make this Notebook Trusted to load map: File -> Trust Notebook
#show heatmap 
from folium import plugins

map = folium.Map(location=[meanLat, meanLon], tiles="Cartodb dark_matter", zoom_start=17)

heatData = [[point.xy[1][0], point.xy[0][0]] for point in obsGeoDf.geometry]

heatData
plugins.HeatMap(heatData).add_to(map)

map
Make this Notebook Trusted to load map: File -> Trust Notebook
#export data to shapefile
obsGeoDf
obsGeoDf = obsGeoDf.reset_index(drop=True)
obsGeoDf.to_file('output/obsGeoDf.shp')
Comment

Saul Montoya

Saul Montoya es Ingeniero Civil graduado de la Pontificia Universidad Católica del Perú en Lima con estudios de postgrado en Manejo e Ingeniería de Recursos Hídricos (Programa WAREM) de la Universidad de Stuttgart con mención en Ingeniería de Aguas Subterráneas y Hidroinformática.

 

Suscribe to our online newsletter

Subscribe for free newsletter, receive news, interesting facts and dates of our courses in water resources.