-
-
Save jatorre/1678789 to your computer and use it in GitHub Desktop.
--First, this is quick hack ported from https://github.com/fruminator/openstreetblock/ | |
--Replace the latitude and longitude accordly on the different places and encode the whole SQL or it will not work | |
http://vizzuality.cartodb.com/api/v1/sql?q= | |
select l.name, l.osm_id from osm_line l join way_nodes wn on l.osm_id = wn.way_id where wn.node_id = ( select n.id from nodes n where ARRAY[n.id::integer] && (select w.nodes from osm_line l join osm_ways w on l.osm_id = w.id where st_intersects(way , ST_buffer(ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326), .002)) order by way <-> ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326) limit 1) order by n.geom <-> ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326) limit 1) and wn.way_id != ( select osm_id from osm_line l join osm_ways w on l.osm_id = w.id where st_intersects(way, ST_buffer(ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326), .002) ) LIMIT 1) and l.name is not null and l.name != '' | |
--example | |
http://vizzuality.cartodb.com/api/v1/sql?q=select%20l.name%2C%20l.osm_id%20%20%20from%20osm_line%20l%20%20%20join%20way_nodes%20wn%20%20%20on%20l.osm_id%20%3D%20wn.way_id%20%20%20where%20wn.node_id%20%3D%20(%20%20%20%20select%20n.id%20%20%20from%20nodes%20n%20%20%20where%20ARRAY%5Bn.id%3A%3Ainteger%5D%20%26%26%20(select%20w.nodes%20from%20osm_line%20l%20join%20osm_ways%20w%20on%20l.osm_id%20%3D%20w.id%20%20%20%20where%20%20%20%20%20%20%20%20%20st_intersects(way%20%20%20%20%20%20%20%20%20%2C%20ST_buffer(ST_SetSRID(ST_MakePoint(-73.999548%2C40.71954)%2C4326)%2C%20.002))%20order%20by%20way%20%3C-%3E%20ST_SetSRID(ST_MakePoint(-73.999548%2C40.71954)%2C4326)%20%20%20%20%20%20%20limit%201)%20order%20by%20n.geom%20%3C-%3E%20ST_SetSRID(ST_MakePoint(-73.999548%2C40.71954)%2C4326)%20%20%20limit%201)%20and%20wn.way_id%20!%3D%20(%20%20%20select%20osm_id%20from%20osm_line%20l%20%20%20join%20osm_ways%20w%20on%20l.osm_id%20%3D%20w.id%20%20%20where%20st_intersects(way%2C%20ST_buffer(ST_SetSRID(ST_MakePoint(-73.999548%2C40.71954)%2C4326)%2C%20.002)%20%20%20)%20LIMIT%201)%20%20%20%20%20and%20l.name%20is%20not%20null%20and%20l.name%20!%3D%20'' | |
--- Here is in multiple lines | |
select l.name, l.osm_id | |
from osm_line l | |
join way_nodes wn | |
on l.osm_id = wn.way_id | |
where wn.node_id = ( | |
select n.id | |
from nodes n | |
where ARRAY[n.id::integer] && (select w.nodes from osm_line l | |
join osm_ways w on l.osm_id = w.id | |
where | |
st_intersects(way | |
, ST_buffer(ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326), .002) | |
) | |
order by way <-> ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326) | |
limit 1) | |
order by n.geom <-> ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326) | |
limit 1 | |
) | |
and wn.way_id != ( | |
select osm_id from osm_line l | |
join osm_ways w on l.osm_id = w.id | |
where | |
st_intersects(way | |
, ST_buffer(ST_SetSRID(ST_MakePoint(-73.999548,40.71954),4326), .002) | |
) LIMIT 1) | |
and l.name is not null and l.name != '' |
Hello,
I've found this really useful, thanks! However in trying to host it on my own cartodb instance I've come across a few problems. The latest one I'm not sure how to get around. The column nodes in the osm_ways table is an array of integer but when I've tried to import it to cartodb it puts it as a string and can't seem to handle integer arrays. So the query fails with this error:
{"error":["operator does not exist: double precision = character varying"]}
How is the column stored in the vizzuality instance?
Thanks,
Peter.
Hi Javier,
I've managed to get the integer[] data type into cartodb now using the SQL API so thanks for that.
The query is still having problems running and I think I narrowed it down to this part of the statement:
...where ARRAY[n.id::integer] && (select w.nodes from osm_line l join osm_ways w on l.osm_id = w.id where st_intersects(l.the_geom, ST_buffer(ST_SetSRID(ST_MakePoint(-1.911986,52.48534),4326), .002)) order by l.the_geom <-> ST_SetSRID(ST_MakePoint(-1.911986,52.48534),4326) limit 1)
It reports an error: argument of WHERE must be type boolean, not type integer[]. Running the same query directly in PostGRES works fine though and I get the correct results.
Thanks again,
Peter.
It's working now! Sorry about that, I'm not sure what the problem was - might have been because I was trying to request it unencoded before.
Thanks again for all your help, this is going to be really useful for us.
Peter.
We are not finished yet :) When I try it for my apartment location:
http://vizzuality.cartodb.com/api/v1/sql?q=select%20distinct%20l.name%2C%20l.osm_id%20from%20osm_line%20l%20join%20way_nodes%20wn%20on%20l.osm_id%20%3D%20wn.way_id%20where%20wn.node_id%20%3D%20%28%20select%20n.id%20from%20nodes%20n%20where%20ARRAY%5Bn.id%3A%3Ainteger%5D%20%26%26%20%28select%20w.nodes%20from%20osm_line%20l%20%20%20%20%20%20%20join%20osm_ways%20w%20on%20l.osm_id%20%3D%20w.id%20%20%20%20%20where%20%20%20%20%20%20%20st_intersects%28way%20%20%20%20%20%20%20%2C%20ST_buffer%28ST_SetSRID%28ST_MakePoint%28-73.97635%2C%2040.68121%29%2C4326%29%2C%20.002%29%20%20%20%20%20%29%20%20%20%20%20order%20by%20way%20%3C-%3E%20ST_SetSRID%28ST_MakePoint%28-73.97635%2C%2040.68121%29%2C4326%29%20%20%20%20%20limit%201%29%20order%20by%20n.geom%20%3C-%3E%20ST_SetSRID%28ST_MakePoint%28-73.97635%2C%2040.68121%29%2C4326%29%20limit%201%20%29%20and%20wn.way_id%20%21%3D%20%28%20select%20osm_id%20from%20osm_line%20l%20%20join%20osm_ways%20w%20on%20l.osm_id%20%3D%20w.id%20%20where%20%20%20st_intersects%28way%20%20%20%20%2C%20ST_buffer%28ST_SetSRID%28ST_MakePoint%28-73.97635%2C%2040.68121%29%2C4326%29%2C%20.002%29%20%29%20LIMIT%201%29%20%20and%20l.name%20is%20not%20null%20and%20l.name%20%21%3D%20%27%27
I get:
"Bergen Street";5681491
"5th Avenue";46865766
"Bergen St";46865767
"5th Avenue";68523765
Both streets twice! Might have to do a distinct on the name or something, going to keep looking at it. By comparison:
http://api.taxono.my:3000/osb?lat=40.68121&lon=-73.97635 =
{
mainStreet: {
name: "Bergen St"
},
inBetween: [
{
name: "5th Ave"
},
{
name: "Flatbush Ave"
}
]
}