-
-
Notifications
You must be signed in to change notification settings - Fork 188
GasStationsOverlay.java #1532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
catmraw34
wants to merge
5
commits into
onthegomap:main
Choose a base branch
from
catmraw34:gasstations-overlay
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
GasStationsOverlay.java #1532
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67e7df9
GasStationsOverlay.java for Customizable Planetiler Profiles and MapL…
catmraw34 00ff224
Add GasStationsOverlay.java example with screenshots of simple map m…
catmraw34 1d0b24a
getTag changed to getString, features.point/polygon changed to featur…
catmraw34 0e04763
Change default area from Monaco to Luxembourg
catmraw34 d670fff
Remove unused checkIfOpen method from GasStationsOverlay
catmraw34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
148 changes: 148 additions & 0 deletions
148
planetiler-examples/src/main/java/com/onthegomap/planetiler/examples/GasStationsOverlay.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package com.onthegomap.planetiler.examples; | ||
|
|
||
| import com.onthegomap.planetiler.FeatureCollector; | ||
| import com.onthegomap.planetiler.Planetiler; | ||
| import com.onthegomap.planetiler.Profile; | ||
| import com.onthegomap.planetiler.config.Arguments; | ||
| import com.onthegomap.planetiler.reader.SourceFeature; | ||
| import java.nio.file.Path; | ||
| import java.time.LocalDateTime; | ||
| import java.time.DayOfWeek; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * this example profile shows gasstations on the map and hours theyre open at. | ||
| * href="https://wiki.openstreetmap.org/wiki/Tag:amenity%3Dfuel" | ||
| */ | ||
|
|
||
| public class GasStationOverlay implements Profile { | ||
|
|
||
| @Override | ||
| public void processFeature(SourceFeature sourceFeature, FeatureCollector features) { | ||
| // Check if this feature is a gas station (amenity=fuel) | ||
| boolean isGasStation = sourceFeature.hasTag("amenity", "fuel"); | ||
|
|
||
| if (isGasStation) { | ||
| // Get the brand name - use as name if name tag is missing | ||
| String brand =(String) sourceFeature.getTag("brand"); | ||
| String name = (String) sourceFeature.getTag("name"); | ||
|
catmraw34 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // If name is missing but brand exists, use brand as the name | ||
| String displayName = (name != null && !name.isEmpty()) ? name : brand; | ||
|
|
||
| // Get opening hours and determine if currently open | ||
| String openingHours =(String) sourceFeature.getTag("opening_hours"); | ||
| String openStatus = checkIfOpen(openingHours); | ||
|
|
||
| // Handle nodes (point at center of gas station) | ||
| if (sourceFeature.isPoint()) { | ||
| features.point("gas_station") | ||
| .setAttr("name", displayName) | ||
| .setAttr("brand", brand) | ||
| .setAttr("operator", sourceFeature.getTag("operator")) | ||
| .setAttr("hgv", sourceFeature.getTag("hgv")) // Large vehicle access | ||
| .setAttr("opening_hours", openingHours) | ||
| .setAttr("open_status", openStatus) // "open", "closed", or "unknown" | ||
| .setMinZoom(12); | ||
| } | ||
| // Handle areas (polygon around fueling area) | ||
| else if (sourceFeature.canBePolygon()) { | ||
| features.polygon("gas_station") | ||
| .setAttr("name", displayName) | ||
| .setAttr("brand", brand) | ||
| .setAttr("operator", sourceFeature.getTag("operator")) | ||
| .setAttr("hgv", sourceFeature.getTag("hgv")) | ||
| .setAttr("opening_hours", openingHours) | ||
| .setAttr("open_status", openStatus) | ||
| .setMinZoom(12); | ||
|
catmraw34 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Simple check if a gas station is currently open based on opening_hours tag. | ||
| */ | ||
| private String checkIfOpen(String openingHours) { | ||
|
catmraw34 marked this conversation as resolved.
Outdated
|
||
| if (openingHours == null || openingHours.isEmpty()) { | ||
| return "unknown"; | ||
| } | ||
|
|
||
| // Check for 24/7 | ||
| if (openingHours.equals("24/7") || openingHours.equals("24-7")) { | ||
| return "open"; | ||
| } | ||
|
|
||
| // Get current time | ||
| LocalDateTime now = LocalDateTime.now(); | ||
| DayOfWeek today = now.getDayOfWeek(); | ||
| int currentHour = now.getHour(); | ||
| int currentMinute = now.getMinute(); | ||
| double currentTime = currentHour + (currentMinute / 60.0); | ||
|
|
||
| // Simple pattern matching for common formats | ||
| String hoursLower = openingHours.toLowerCase(); | ||
|
|
||
| // Check if closed on this day | ||
| String[] days = {"mo", "tu", "we", "th", "fr", "sa", "su"}; | ||
| String todayAbbr = days[today.getValue() - 1]; // Monday=1 -> index 0 | ||
|
|
||
| if (hoursLower.contains(todayAbbr + " off") || hoursLower.contains(todayAbbr + " closed")) { | ||
| return "closed"; | ||
| } | ||
|
|
||
| // Check for simple time ranges like "Mo-Fr 08:00-20:00" | ||
| Pattern timePattern = Pattern.compile(todayAbbr + "[^0-9]*([0-9]{1,2}):?([0-9]{2})?-([0-9]{1,2}):?([0-9]{2})?"); | ||
| var matcher = timePattern.matcher(hoursLower); | ||
|
|
||
| if (matcher.find()) { | ||
| int openHour = Integer.parseInt(matcher.group(1)); | ||
| int closeHour = Integer.parseInt(matcher.group(3)); | ||
| double openTime = openHour; | ||
| double closeTime = closeHour; | ||
|
|
||
| if (currentTime >= openTime && currentTime < closeTime) { | ||
| return "open"; | ||
| } else { | ||
| return "closed"; | ||
| } | ||
| } | ||
|
|
||
| // Check for "sunrise-sunset" (always treat as open - would need external data for real calculation) | ||
| if (hoursLower.contains("sunrise") || hoursLower.contains("sunset")) { | ||
| return "unknown"; | ||
| } | ||
|
|
||
| // Default for complex formats not parsed | ||
| return "unknown"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isOverlay() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return "Gas Stations Overlay"; | ||
| } | ||
|
|
||
| @Override | ||
| public String description() { | ||
| return "Shows gas stations including brand, operator, HGV access, and opening status"; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| run(Arguments.fromArgsOrConfigFile(args)); | ||
| } | ||
|
|
||
| static void run(Arguments args) { | ||
| String area = args.getString("area", "geofabrik area to download", "monaco"); | ||
|
|
||
| Planetiler.create(args) | ||
| .setProfile(new GasStationOverlay()) | ||
| .addOsmSource("osm", Path.of("data", "sources", area + ".osm.pbf"), "geofabrik:" + area) | ||
| .overwriteOutput(Path.of("data", "gasstations.mbtiles")) | ||
| .run(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.