Skip to content

Instantly share code, notes, and snippets.

@achisto
Forked from kevinkub/incidence.js
Last active October 21, 2020 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achisto/3acc7e2f7e0bd42ae4959d01f430878f to your computer and use it in GitHub Desktop.
Save achisto/3acc7e2f7e0bd42ae4959d01f430878f to your computer and use it in GitHub Desktop.
COVID-19 Inzidenz-Widget für iOS innerhalb Deutschlands 🇩🇪
// Licence: Robert Koch-Institut (RKI), dl-de/by-2-0
// Forked from Kevin Kub: https://gist.github.com/kevinkub/46caebfebc7e26be63403a7f0587f664
// Some parts copied from fork by @ rphl: https://gist.github.com/rphl/0491c5f9cb345bf831248732374c4ef5
const apiUrl = (location) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=GEN,cases7_per_100k&geometry=${location.longitude.toFixed(3)}%2C${location.latitude.toFixed(3)}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json`
const apiUrlLand = (landname) => `https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/Coronaf%C3%A4lle_in_den_Bundesl%C3%A4ndern/FeatureServer/0/query?where=LAN_ew_GEN+%3D+%27${encodeURIComponent(landname)}%27&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=LAN_ew_GEN%2Cfaelle_100000_EW%2Ccases7_bl_per_100k&returnGeometry=false&returnCentroid=false&featureEncoding=esriDefault&multipatchOption=none&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token=`
let widget = await createWidget()
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
let location
if(args.widgetParameter) {
const fixedCoordinates = args.widgetParameter.split(",").map(parseFloat)
location = {
latitude: fixedCoordinates[0],
longitude: fixedCoordinates[1]
}
} else {
Location.setAccuracyToThreeKilometers()
location = await Location.current()
}
const data = await new Request(apiUrl(location)).loadJSON()
if(!data || !data.features || !data.features.length) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.")
return errorList
}
const attr = data.features[0].attributes
const incidence = attr.cases7_per_100k.toFixed(1)
const cityName = attr.GEN
const list = new ListWidget()
let incidenceLand;
let landName;
if (!attr.BL) {
console.log('Kein Bundesland', attr);
} else {
const dataLand = await new Request(apiUrlLand(attr.BL)).loadJSON()
if(!dataLand || !dataLand.features || !dataLand.features.length) {
const errorList = new ListWidget()
errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.")
return errorList
}
const attrLand = dataLand.features[0].attributes
incidenceLand = attrLand.cases7_bl_per_100k.toFixed(1)
landName = attrLand.LAN_ew_GEN
}
const header = list.addText("🦠 Inzidenz".toUpperCase())
header.font = Font.mediumSystemFont(13)
list.addSpacer()
const label = list.addText(incidence+"")
label.font = Font.boldSystemFont(24)
label.textColor = Color.green()
// if(incidence >= 50) {
// label.textColor = Color.red()
// } else if(incidence >= 35) {
// label.textColor = Color.orange()
// }
list.addText(cityName)
list.addSpacer()
let landLabel;
if (typeof landName !== 'undefined' && typeof incidenceLand !== 'undefined') {
landLabel = list.addText(incidenceLand + " " +landName)
} else {
landLabel = list.addText("Bundesland konnte nicht ermittelt werden.")
}
landLabel.font = Font.systemFont(12)
landLabel.textColor = Color.gray()
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment