How convert OSM relation to GeoJSON with LineString? - openstreetmap

I want to get the data for a relation from the overpass turbo API, convert it to GeoJSON and get the LineString. What script/library should I use? I am using the query below to get the data.
[out:json][timeout:300];
(
relation(19763);
<;
);
out geom;
I have tried using osmtogejosn.js but it doesn't return the same result as the overpass-turbo.eu export function.

Related

Get separate layers in an mvt

Our platform currently uses pbf tiles(on s3) to render vector layers on Mapbox on the frontend. The pbf tiles have separate layers within them and we use Mapbox layers' source-layer property to style them as separate layers. We are now moving to PostGIS and we want to avoid s3 and directly render the vector tiles as mvt on the frontend.
I am trying to get separate layers from the mvt but the only layer I see is a 'default' layer. I identified this using this vizualizer tool. I am currently getting the mvt using this query -
WITH mvtgeom AS (
SELECT ST_AsMVTGeom(ST_Transform(ST_Force2D(geom), 3857),
ST_TileEnvelope(${z}, ${x}, ${y}))
AS geom,
layer,
gid
FROM site-name
WHERE ST_Intersects(geom, ST_Transform(ST_TileEnvelope(${z}, ${x}, ${y}), 4326)))
SELECT ST_AsMVT(mvtgeom.*) AS mvt FROM mvtgeom;
This issue is very close to my problem but the answer suggests using a UNION, which I want to avoid if possible considering we have to render hundreds of layers.
I also tried this. In this case, when I put the values of my layer-ids into an IN, I see that those set of layers are rendered as one layer.
(Is there something I need to explore in order to understand how vector tiles are generated so I know how exactly the separate layers are generated? I am new to PostGIS, and I'm not sure if I'm doing something wrong or missing something. Any suggestions are appreciated)
Below is the full return signature of ST_AsMvt
bytea ST_AsMVT(anyelement row, text name, integer extent, text geom_name, text feature_id_name);
Later in the docs:
name is the name of the layer. Default is the string "default".
Try using the id (or label, or some other thing specific to each layer) to create a distinct layer name for each layer:
WITH mvtgeom AS (
SELECT
ST_AsMVTGeom(ST_Transform(ST_Force2D(geom), 3857) AS geom
, layer
, gid
FROM
site-name
WHERE
ST_Intersects(
geom,
ST_Transform(ST_TileEnvelope(${z}, ${x}, ${y}), 4326)
)
)
SELECT
ST_AsMVT(
mvtgeom.*,
'layer_' || layer -- this is the layer name argument
) AS mvt
FROM
mvtgeom
GROUP BY
layer;
This name property, by the way, is what mapbox uses as the "source layer"

OSM API Overpass

I am trying to pull all glaciers as entered in OSM in a given country but am noticing that I am only pulling a fraction of what is available. For example, when I run this following code:
import overpass from shapely.geometry
import shape, Polygon
api = overpass.API()
api = overpass.API(endpoint="https://overpass.myserver/interpreter")
api = overpass.API(timeout=600)
query = 'area["ISO3166-1"="IS][admin_level=2];(way["natural"="glacier"](area););'
result = api.get(query, verbosity='geom')
import geopandas
results = geopandas.GeoDataFrame.from_features(result['features'])
The result has 132 features and appears as so:
Iceland Glaciers
I know this is missing one large glacier (Vatnajökull) which does appear in OSM under osm id 406429.
Any thoughts as to why this is not appearing as a result from my query?
OSM Wiki tag documentation is a helpful starting point when writing Overpass queries. Here is the documentation for natural=glacier. The tag/value is applied to nodes and closed ways based on the documentation and also appears to apply to relations based on community preference (even though this is discouraged in the documentation).
To query for nodes, ways, and relations, you can use the abbreviation nwr instead of the union (node[natural=glacier];way[natural=glacier];relation[natural=glacier];);. As a side note, you can drop admin_level=2 since ISO3166-1 codes are unique identifiers.
Here is the Python request:
query = 'area["ISO3166-1"="IS"];nwr[natural=glacier](area);out geom;'
response = api.get(query)

How to export the roadmap including traffic_signals and street_lamps of a city?

I would like to get some statistics about the roads, their deployed traffic lights, and the lampposts. Is there any way to get these statistics immediately for Shenzhen (China) city?
Secondly: how can I export the road network of a specific city (i.e., Shenzhen) including traffic_signals and street_lamps?
I have tried this code using Overpass API:
[out:csv(::id,::lat,::lon)][timeout:900];
// gather results
(
node["highway"="street_lamp"](22.6242,113.6371,23.0628,114.5462);
);
// print results
out body;
The query doesn't retrieve any results for Shenzhen's(China) coordinates(22.6242,113.6371,23.0628,114.5462).However, when applying on coordinates of London(51.3941,-0.2774,51.56,0.0879), it works and retrieves.
Moreover, when I do simple query like querying PoI:
[out:json][timeout:10];
// gather results
(
node["leisure"](around: 200,22.5,113.9936701,22.6740047,113.9935278);
);
out body;
It also works although in Shenzhen(China). Any way to retrieve nodes tagged with 'street_lamp' and 'traffic_sign' in Chinese cities (i.e., Shenzhen)?
To query within boundaries, use the id of the city boundary's relation, then use map_to_area and then query with the the (area) filter:
rel(3464353);
map_to_area;
node(area)["highway"="street_lamp"];
out;

Export multiple .gpkg polygons as a shapefiles using a particular field name

I split polygons into separate polygons that where connected. when I ran the function vector/data management tools/split vector layers. it created .gpkg files. i want to export from QGIS as shapefiles and name them from a field in the attribute table . is there a function in QGIS to do a bulk export of individual polygons and rename based on field ?
I used the plugin Bulk vector exporter and used lupus re name

PostGIS latitude incorrect when storing a linestring

I am attempting to store a LINESTRING using PostGIS into a column of type geography(LINESTRING, 4326). Here is my insert statement:
INSERT into routes (line) VALUES (st_linefromtext('LINESTRING(-35.3350743932 149.084182978,-35.3350306311 149.085041285)', 4326));
But when I run a query to get the line back out of the database.
SELECT st_astext(line) from routes;
Result:
LINESTRING(-35.3350743932 30.915817022,-35.3350306311 30.914958715)
The latitude coordinates come out completely differently from how I inputted them. Can anyone point out to me why this would be?
I am new to PostGIS - I think I must be missing something about the storage and retrieval of 4326 data. Any help appreciated.