Skip to content

Commit

Permalink
OSM: implement getPhotos for images hosted at imgur
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Jul 9, 2023
1 parent f256ddc commit 6fb3107
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.vonforst.evmap.api.openstreetmap

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.parcelize.Parcelize
import net.vonforst.evmap.model.*
import okhttp3.internal.immutableListOf
import java.time.Instant
Expand Down Expand Up @@ -105,7 +106,7 @@ data class OSMChargingStation(
tags["description"],
null,
null,
null,
getPhotos(),
null,
getOpeningHours(),
getCost(),
Expand Down Expand Up @@ -200,6 +201,25 @@ data class OSMChargingStation(
return Cost(freecharging, freeparking, null, description)
}

private fun getPhotos(): List<ChargerPhoto> {
val photos = mutableListOf<ChargerPhoto>()
for (i in -1..9) {
val url = tags["image" + if (i >= 0) ":$i" else ""]
if (url != null) {
if (url.startsWith("https://i.imgur.com")) {
ImgurChargerPhoto.create(url)?.let { photos.add(it) }
}
/*
TODO: Imgur seems to be by far the most common image hoster (650 images),
followed by Mapillary (450, requires an API key to retrieve images)
Other than that, we have Google Photos, Wikimedia Commons (100-150 images each).
And there are some other links to various sites, but not all are valid links pointing directly to a JPEG file...
*/
}
}
return photos
}

companion object {
/**
* Parse raw OSM output power.
Expand All @@ -222,4 +242,26 @@ data class OSMChargingStation(
return numberString.toDoubleOrNull()
}
}
}
}

@Parcelize
@JsonClass(generateAdapter = true)
class ImgurChargerPhoto(override val id: String) : ChargerPhoto(id) {
override fun getUrl(height: Int?, width: Int?, size: Int?, allowOriginal: Boolean): String {
return if (allowOriginal) {
"https://i.imgur.com/$id.jpg"
} else {
val value = width ?: size ?: height
"https://i.imgur.com/${id}_d.jpg?maxwidth=$value"
}
}

companion object {
private val regex = Regex("https?://i.imgur.com/([\\w\\d]+)(?:_d)?.(?:webp|jpg)")

fun create(url: String): ImgurChargerPhoto? {
val id = regex.find(url)?.groups?.get(1)?.value
return id?.let { ImgurChargerPhoto(it) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.squareup.moshi.Types
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import net.vonforst.evmap.api.goingelectric.GEChargerPhotoAdapter
import net.vonforst.evmap.api.openchargemap.OCMChargerPhotoAdapter
import net.vonforst.evmap.api.openstreetmap.ImgurChargerPhoto
import net.vonforst.evmap.autocomplete.AutocompletePlaceType
import net.vonforst.evmap.model.ChargeCardId
import net.vonforst.evmap.model.Chargepoint
Expand All @@ -23,6 +24,7 @@ class Converters {
PolymorphicJsonAdapterFactory.of(ChargerPhoto::class.java, "type")
.withSubtype(GEChargerPhotoAdapter::class.java, "goingelectric")
.withSubtype(OCMChargerPhotoAdapter::class.java, "openchargemap")
.withSubtype(ImgurChargerPhoto::class.java, "imgur")
.withDefaultValue(null)
)
.build()
Expand Down

0 comments on commit 6fb3107

Please sign in to comment.