How can I associate a leaflet marker with an object id in my database - leaflet

On a webpage with leaflet maps - I want to be able to display my database objects, each of which have a lat,lng. using markers on the map.
So when I click the marker, I would like to fetch information from db and display this.
How can I store the object id or associate it with a marker so that when a marker is clicked I can load my object from database and display detailed information?
I know I could use lat/lng to find the object using radius search or something, but this won't work and is not accurate, as there could be more than one object with same lat/lng.

Ok based on the answer provided by #Ivar in another thread: Leaflet: Add a link to the markers
You can associate a dbObject Id to the marker directly, after the marker is added to the map. when a marker is added to the map it gets assigned an ID called "_leaflet_id". This can be fetched through the target object, and also set to a custom value after it has been added to the map.
Now in the marker OnClick event you can just get the clicked marker's id and use it to query the database.
marker._leaflet_id = dbObjectId;

Not sure how you are connecting to your db, but the flow would be something like this:
grab the clicked point's lat/lng, then send that info to some logic that employs a spatial geometry lib like S2 or something. Create an envelope around the clicked point, and get all the points whose lat/lngs are within that envelope, then whichever's lat/lng is the shortest spherical distance to your clicked point is your match.
I am sure there is an easier way to do this through CartoDB or something like PostGIS.
--
Assuming your database objects are set up in a table like this:
ID(guid) | Name | Latitude | Longitude
fe124etc.| Marker1| 43.123 | -117.123
y75W2etc.| Marker2| 44.123 | -116.123
you do the method above and then once you have the marker you want, you send the other db data (name, address, w/e) back to a popup that is bound to the marker.
Another avenue to explore is (depending on the data) you can make a geoJSON layer out of it. (leaflet geojson use). If you do that, you can create/store/read attributes for each point.
Hope this makes it a little more clear.

Related

Maplibre : Access 'way' and 'relation' ID from the mbtiles on the map - queryRenderedFeatures

I'm using the queryRenderedFeatures function from https://maplibre.org/maplibre-gl-js-docs/example/queryrenderedfeatures/ to query all features over the mouse on a MapLibre map.
I have access to all Node, and the id identified in the windows looks similar to the OSM ID (where the mbtiles seems to add a '0' at the end of the OSM ID :
mbtiles ID : 1230 corresponds to OSM ID N123)
(A) When I'm trying to access Way, the ID doesn't show up in the queryRenderedFeatures function
A console.log of the queried feature shows the ID as undefined
When I compare to the query on OpenStreetMap website, I can confirm that both return similar results by re-building the LineString output from MapLibre.
In other words, the query looks good and accurate, but I'm curious on how to fetch the OSM_ID from a way with this function ?
The source ID is road_path_pedestrian, with its source layer being Transportation for which I've added the osm_id (in case this could have been the root cause)
(B) as for the relation, is there any way to fetch those from the mbtiles (rendered or not) directly on the map ?
In the example above, the summit track has a relation on OSM that gathers all portions of the summit track ways. Is there a way to access as well the ID of this relation from the mbtiles, on the go ?
The objective is to have the user clicking on a path/track on a maplibre map, and the function to return the full OSM relation ID and LineString.
Exactly the same way it's currently done on OpenStreetMap website. For the same example, it would be the yellow track with its 7 members.
Thanks in advance for your support!

Efficiant loading via filter with Mapbox

I have several mapbox datasets, each with a collection of features located in different cities. Each feature has a field named city that contains the city the feature is located in (e.g. city = "new york"). In mapbox studio I have a style with layers made from each of those datasets. I want to load a map with it's bounding box containing all features with city == "new york"(fit map view to features bbox). How can I accomplish that, preferably without ever loading features that don't satisfy city=="new york"
If I understand you correctly, you have a basemap that contains all the cities, but when you load the map in mapbox-gl-js, you want to filter them to just one particular city.
You do this by calling:
map.setFilter('my-cities-layer', ['==', ['get', 'city'], 'new york']);
There isn't really a simple way of filtering all layers to only include data with that tag, though.
After selecting the relevant layer in Mapbox Studio, select the property whose visibility you would like to control. For example, suppose that you are interested in showing a text field for each of these features in your dataset (the same logic could also be applied to icon images). Once this property of the layer is selected, click on the "Style with data conditions" option in the layer editor. You will then be prompted to choose a data field (which in your case will be city) and subsequently prompted to set values for this data field (which in your case will be new york).

How to store markers (with id) and populate map for each user using mongodb?

I'm a complete noob so bear with me please -
Currently users can add markers and my plan is to use the ondraw event to get the marker lat/lng and store that in mongodb, however i don't know how to get the individual markerID - ._leaflet_id returns same id for every marker.
But if i generate id for the marker using something like Date.now() how can i fetch that id from db if a user clicks on a marker? Each marker needs to be unique since each one will have a different redirect for onclick().
Thank you
I had a similar issue sometime back. What I did was create a table with [unique key id] and another column as markerJson,
var marker = new L.Marker([0, 0]);
// Get the GeoJSON object
var geojson = marker.toGeoJSON();
which has the following
{
"type":"Feature",
**"properties":{},**
"geometry":{
"type":"Point",
"coordinates":[0,0]
}
}
Save the above json in markerjson Column created above.
You can see in the json above the properties feature "properties":{}, does the logic
from db fetch and append the [unique key id] to properties "properties":{} e.g
"properties":{unique-key_id:28}, now user can access the unique id from properties.
But in the case above on creating an event, the create event has properties{} feature, just append _leaflet_id to properties with the generated Id with Date.now().

Is there an easy way to Map properties on Socrata?

Since some of the open data has to do with property and each city has a unique identifier for a property, is there an easy way to map those unique identifiers in Socrata?
Visually, given a list of property unique identifiers in the open data, I'd like to see the properties on a map in one step.
More examples: If you go to http://api.us.socrata.com/api/catalog/v1?column_names=bbl you will see hundreds of references because this is what New York City calls it's unique identifier for a property.
What would be helpful is if we could easily see these properties on a map. We know that datasets should have latitude and longitude, but they usually do not. Plus for a property it is better to see the outline of the property and not just a point on a map.
In order to make it easier to do this, there is a tool at https://map.tidalforce.org/ it allows you to paste a comma delimited NYC BBLs (borough, block and lot) list. For example, https://map.tidalforce.org/?1pNFsY5rnHcsFEpLW2CJ this maps the socrata dataset at https://data.cityofnewyork.us/City-Government/Open-Article-7-Petitions/aht6-vxai
Is there a way to integrate with Socrata to make it easier to map properties for each area, city, state, etc. Or does this functionality already exist.

How to set street obstructed in planet_osm_line/pg_route

I am working on a project where we are going to be looking at finding the shortest/fastest route from point A to point B. I've been looking at the tables generated by the osm2pgsql. And I'm wondering how would I represent a road obstructed after the osm has been loaded into our database. Our project will rely on osm to map out all of the roads we will also have an operator looking at live video footage of roads. At which point if the operator see's a road is obstructed we want to update the database to reflect this road obstructed say by a downed tree.
I've been looking at all of the columns and the only one that stands out in my head is barrier. I have been unable to find any documentation on what each column represents and how pg_route takes each into consideration when creating a route. What I'm looking for is a column that when pg_route looks in the database and sees a road it says oh that roads blocked skip it?
This is good question for gis.se...
First thing is pg_routing can't route via data generated by osm2pgsql - this data is not a network. You need data generated by osm2po or osm2pgrouting and this data is quite different.
Second thing is - there is no such column. In every pg_routing function you're passing sql which will select data for route search so you're deciding which edge will be in this dataset and which not - it's not a problem to add extra column to table with edges.
Here is link to pgrouting workshop it will guide you through all process from import of data to first generated route. It's using osm2pgroutin to import data, but I suggest you use osm2po instead.
So as Jendrusk mentioned, when you generate a route you will pass the function a SQL query to select the edges for the graph you want to solve, 'select * from edges where the_geom && <bbox>' You can model blockages using point and radius, lines, or polygons that you want the route to avoid by adding to the query above avoidance zones like:
'select * from edges where the_geom && <bbox> and not st_dwithin(the_geom, point, radius) and not stdwithin(the_geom, line_or_polygon, 0.0)'
If you have lots of these avoidances then put them in a table and do a join to eliminate the edges that are used to build the graph. If the edges are not there the route is forces to find a way around the avoidance.