re-translate the values of bounding-boxes to a certain region

hello dear experts,

can i re-translate the values of bounding-boxes to a certain region

see the following

52.6697240587,13.0882097323,52.3418234221,13.7606105539

eg bavaria
50.5644529365, 8.9771580802,47.2703623267,13.8350427083

paris
2.0868, 48.6583, 2.6379, 49.0469

berlin
52.6697240587, 13.0882097323, 52.3418234221, 13.7606105539

Hamburg
53.9644376366, 8.4213643278, 53.3949251389, 10.3242585128

Frankfurt
8.4727, 50.0153, 8.8004, 50.2272

Stuttgart
9.034,48.687,9.320,48.872

the question is: am i able to re-translate the values of bounding-boxes to a certain region - if i have no name of a region!?

love to hear from you

I don’t understand the question, and the lack of other answers makes me think others don’t either.

One common reason for difficulty is that people use openstreetmap to refer to services (like Garmin maps) on the periphery of OSM, without making it clear that that is what they are talking about. Maybe that is the case here, and explaining what tools or web site you are using may make things clearer.

These look very like the example bounding boxes in a python script to render OSM data using Mapnik. Other than that I can add no more.

I think you’re probably right. I didn’t understand the question.

But having just read it again, maybe the he/she is after is being able to lookup the region containing given co-ordinates. Although why he/she wants to translate a bounding box of co-ordinates rather than just a single co-ordinate eludes me, so that probably isn’t it.

Well, for the tile splitter [1] we have a routine that searches for the next big city for a given bounding box. I think that does
what is wanted. The package is uk.me.parabola.splitter.geo
and it uses a cities list described here:
http://download.geonames.org/export/dump/readme.txt

Gerd
[1] http://www.mkgmap.org.uk/websvn/listing.php?repname=splitter&

If it helps, I use http://tools.geofabrik.de/calc/#type=geofabrik_standard&bbox=10.241269,50.541002,11.797148,51.927214&tab=1&proj=EPSG:4326&places=2 as a general “bounding box helper”.

… but I don’t really understand the question either.

hello and good evening dear Brian de Ford, SK53, hadw,GerdP, SomeoneElse,

first of all - many many thanks for the postings and the ideas.

well - i just have asked the above question in order to shed a light onto a following problem.

the code below fetches some data from the api-endpoint - and extracts it with xpath
as the data looks not very complete - i just wanted to change the corresponding target -

guessing that this does not contain a dataset that stands for fully fledged and tagged list of shools.


$query = 'node
  ["amenity"~".*"]
  (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
out;';

but i gueessed that the above mentioned bounding boxes do… as the would stand for

Berlin and several areas in center Europe.

But wait: when i add the data of the bounding boxes - the script does not work at all - why !?

so i would love to hear from you
please set me straigth if i was wrong in the idea - assuming that i can enter the bounding boxes in to the script. …

love to hear from you
tag the world now

I am new to PHP’s SimpleXML. i want to work with SimpleXML on OSM-files.

The original version of this question was derived from here: OSM Data parsing to get the nodes with child https://stackoverflow.com/questions/16129184/osm-data-parsing-to-get-the-nodes-with-child

I am thankful that hakre offered a great example in the comments that makes a overwhelming
starting point for my project. Below I have added my own answer to the question, how to refine the code to ad more tags. I can work on the methods using SimpleXML and Xpath; The job is most easily done with xpath, the used PHP XML library is based on libxml which supports XPath 1.0 which covers the various querying needs very well.

**goal: **how to get more out of it: I want to filter the data to get the nodes with special category. Here is sample of the OSM data I want to get the whole schools within an area. The first script runs well - but now I want to refine the search and add more tags. Finally I want to store all into MySQL.

So we need to make some XML parsing with PHP:

The following is a little OSM Overpass API example with PHP SimpleXML

should this be added in this part!?


# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

since i am learning - i break down the code into pieces…For my question, the second part is more interesting here.

That is querying the XML data we have already. Again - as mentioned above: This is most easily done with xpath, the used PHP XML library is based on libxml which supports XPath 1.0 which covers the various querying needs very well. The following example lists all schools and tries to obtain their names as well.

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

The key point here are the xpath queries: Two are used, the first one to get the nodes that have certain tags.

//node[tag[@k = "amenity" and @v = "school"]]

This line says: Give me all node elements that have a tag element inside which has the k attribute value “amenity” and the v attribute value “school”. Explanation: This is the condition we have to filter out those nodes that are tagged with amenity school.

Further on xpath is used again - a second time: now relative to those school nodes to see if there is a name and if so to fetch it: Therefore we use the foreach-syntax:


foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

and


tag[@k = "name"]/@v'
= $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];

and this is pretty important


tag[@k = "name"]/@v'

This line says: Relative to the current node, give me the v attribute from a tag element that as the k attribute value “name”. As you can see, some parts are again similar to the line before. I think you can both adopt them to your needs.

Because not all school nodes have a name, a default string is provided for display purposes by adding it to the (then empty) result array:


list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
                                                    ^^^^^^^^^^^^^^^
                                                Provide Default Value

So here some of the results for that code-example:


Query returned 907 node(s) and took 1.10735 seconds.
more than 2000 School(s) found:
#00: ID:332534486   [39.5017565,16.2721899]  Scuola Primaria
#01: ID:1428094278  [39.3320912,16.1862820]  (unnamed)
#02: ID:1822746784  [38.9075566,16.5776597]  (unnamed)
#03: ID:1822755951  [38.9120272,16.5713431]  (unnamed)
#04: ID:1903859699  [38.6830409,16.5522243]  Liceo Scientifico Statale A. Guarasci
#05: ID:2002566438  [39.1347698,16.0736924]  (unnamed)
#06: ID:2056891127  [39.4106679,16.8254844]  (unnamed)
#07: ID:2056892999  [39.4124687,16.8286119]  (unnamed)
#08: ID:2272010226  [39.4481717,16.2894353]  SCUOLA DELL'INFANZIA SAN FRANCESCO
#09: ID:2272017152  [39.4502366,16.2807664]  SCUOLA MEDIA 

and now i try to figure out how i can enter more xpath queries at the above mentioned code

goal: to get out even more important data - see here Key:contact - OpenStreetMap Wiki

Well - we are already extracting the name: If we want to have more data then we just have to run a few more xpath queries inside our loop for all the address keys and the website. So - additionally: we do not have to forget to look for the website key additional to contact:website. cf: https://wiki.openstreetmap.org/wiki/Key:website

**conclusio: **well - i think that i need to extend the xpath requests within the loop where xpath is used again, now relative to those school nodes to see if there is a name and if so to fetch it:


tag[@k = "name"]/@v'
tag[@k = "contact:website"]/@v'
tag[@k = "contact:email"]/@v'

What do you say…?

i did some further tess and found out very interesting things

see more here: - the code that runs very well:


#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''#
<?php
/**
 * OSM Overpass API with PHP SimpleXML / XPath
 *
 * PHP Version: 5.4 - Can be back-ported to 5.3 by using 5.3 Array-Syntax (not PHP 5.4's square brackets)
 */
//
// 1.) Query an OSM Overpass API Endpoint
//

$query = 'node
  ["amenity"~".*"]
  (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
out;';

$context = stream_context_create(['http' => [
    'method'  => 'POST',
    'header' => ['Content-Type: application/x-www-form-urlencoded'],
    'content' => 'data=' . urlencode($query),
]]);

# please do not stress this service, this example is for demonstration purposes only.
$endpoint = '[url]http://overpass-api.de/api/interpreter[/url]';
libxml_set_streams_context($context);
$start = microtime(true);

$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));

//
// 2.) Work with the XML Result
//

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}


//node[tag[@k = "amenity" and @v = "school"]]
//tag[@k = "name"]/@v'

$query = 'node
  ["addr:postcode"~"RM12"]
  (51.5557914,0.2118915,51.5673083,0.2369398);
   node
  (around:1000)
  ["amenity"~"fast_food"];
           out;';

$context = stream_context_create(['http' => [
    'method'  => 'POST',
    'header' => ['Content-Type: application/x-www-form-urlencoded'],
    'content' => 'data=' . urlencode($query),
]]);

$endpoint = '[url]http://overpass-api.de/api/interpreter[/url]';
libxml_set_streams_context($context);

$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));


see the results:


me/martin/dev/php/o1.php on line 68
linux-3645:/home/martin/dev/php # php o1.php
Query returned 2799 node(s) and took 17.02055 seconds.

33 School(s) found:
#00: ID:332534486   [39.5018840,16.2722854]  Scuola Elementare
#01: ID:1428094278  [39.3320912,16.1862820]  (unnamed)
#02: ID:1822746784  [38.9075566,16.5776597]  (unnamed)
#03: ID:1822755951  [38.9120272,16.5713431]  (unnamed)
#04: ID:2002566438  [39.1349460,16.0736446]  (unnamed)
#05: ID:2056891127  [39.4106679,16.8254844]  (unnamed)
#06: ID:2056892999  [39.4124687,16.8286119]  (unnamed)
#07: ID:2272010226  [39.4481717,16.2894353]  Scuola dell'infanzia San Francesco
#08: ID:2272017152  [39.4502366,16.2807664]  Scuola Media
#09: ID:2358307794  [39.5015031,16.3905965]  I.I.S.S. Liceo Statale V. Iulia
#10: ID:2358307796  [39.4926280,16.3853662]  Liceo Classico
#11: ID:2358307797  [39.4973761,16.3858275]  Scuola Media
#12: ID:2358307800  [39.5015527,16.3941156]  I.T.C. e per Geometri
#13: ID:2358307801  [39.4983862,16.3807796]  Istituto Professionale
#14: ID:2448031004  [38.6438417,16.3873106]  (unnamed)
#15: ID:2458139204  [39.0803263,17.1291649]  Sacro Cuore
#16: ID:2552412313  [39.0765212,17.1224610]  (unnamed)
#17: ID:2582443083  [39.0815417,17.1178983]  Liceo Socio Biologico Gravina
#18: ID:2585754364  [38.8878393,16.4076323]  Scuola Elementare
#19: ID:2585754366  [38.8877600,16.4076216]  Scuola Media
#20: ID:3071126720  [38.6022703,16.5554408]  Scuola Media
#21: ID:3071127683  [38.6027273,16.5563125]  Scuola Elementare
#22: ID:3081362915  [39.2865638,16.2601963]  Convitto Nazionale Bernardino Telesio
#23: ID:3081362921  [39.2856714,16.2613594]  Liceo Classico B. Telesio
#24: ID:3081362926  [39.2888949,16.2577446]  Scuola
#25: ID:3732551794  [39.5132435,16.2863285]  (unnamed)
#26: ID:3740289655  [39.5167318,16.2838146]  scuola media
#27: ID:3740289656  [39.5164344,16.2821103]  scuola elementare
#28: ID:4004532684  [38.7804787,16.5122952]  Liceo Artistico
#29: ID:4589289756  [38.6794209,16.1063084]  Scuola Comprensiva Trentacapilli
#30: ID:4843966477  [39.0709866,17.1288384]  Pegaso
#31: ID:5297629775  [38.5768845,16.3263536]  Scuola Media Statale "Ignazio La Russa"
#32: ID:5316865306  [39.0807997,17.1264225]  Enrico Fermi
Query returned 3 node(s) and took 17.44780 seconds.


so far so good : if i add some lines in the part 2 i run into errors… -see below:
background: i want to get more data out of the dataset - i wnat to have more information about.

i want to get more data out of it: - and coded like so;


{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
    list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

note - within the part 2 that works with the XML-Result.



//
// 2.) Work with the XML Result
//

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
    list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}


the question is: how to get more out of it… at least the address and the website and now i try to figure out how i can enter more xpath queries at the above mentioned code and get out even more important data - see here Key:contact - OpenStreetMap Wiki

contact:phone
contact:fax    
contact:website
contact:email

I will dig into all documents and come back later the weekend… and report all the findings

well - i think that i need to extend the xpath requests within the loop where xpath is used again, now relative to those school nodes to see if there is a name and if so to fetch it:

tag[@k = “name”]/@v
tag[@k = “contact:website”]/@v
tag[@k = “contact:email”]/@v

You have to understand that OSM is a crowd-sourced project. Therefore, not all schools will have all information.
Some some school might not have the tag contact:website. Maybe they do have the tag website. It is an ongoing discussion which of the 2 tags should be used. The same is true for contact:email vs email.

Please take a look at the raw data in OSM, I searched for “Scuola dell’infanzia San Francesco” and found this way representing the school. There is other information than the name.

here are some schools in Berlin, all with different levels of detail

those all have “complete addresses”, but there is no need to explicitly add city and country to a school neither:

and this one has an isced:level

hello dear escada,

many thanks for the anwer. great to hear from you. sure thing - osm is a crowd-sourced-project i know.

The issues i have had with the test of the above mentioned code was: As we see that the bounding box that is included does not have any set at all - that contains a so called fully-fledged and complete datasets.

So - the first guess was that i have choosen the wrong tag-set

in other words: is this the only tag - sheme for the subsequent finding of website - or not…!?
contact:website.

**
the question is: **how to get more out of it… at least the address and the website and now i try to figure out how i can enter more xpath queries at the above mentioned code and get out even more important data - see here Key:contact - OpenStreetMap Wiki

contact:phone
contact:fax   
contact:website
contact:email

I will dig into all documents and come back later the weekend… and report all the findings

well - i think that i need to extend the xpath requests within the loop where xpath is used again, now relative to those school nodes to see if there is a name and if so to fetch it:

tag[@k = “name”]/@v
tag[@k = “contact:website”]/@v
tag[@k = “contact:email”]/@v

finally: i just wanted to test the above mentioned code with a Berlin area - in order to have a closer look at the derived dataset -

  • guess: this would contain lots of more schools with a so called __ complete___ dataset.

Hope i was able to spell out what is wanted - and aimed. - in short: setting up a controll-group that confirms that the measures of the tagging shemes that i use with the xpath were okay - and able to show results.

At the moment - i think that the above mentioned code does not do what is wanted - since i use the wrong tag-shemes…

Proof of the concept would be to run it in a well worked out are - eg. Berlin or Munich…

Look forward to hear from you

regards tagtheworld :wink:

I haven’t read your code, I just wondered whether any of the schools you retrieve have a contact:website tag at all. If not, you can try to adapt your code as much as you want, but never see a result for that field. That is why I brought up “website” as alternative tag.

So in addition to your program you should check the OSM data like I did in the examples I gave.
My suggestion would be to first look for a field that is in the list of schools you retrieve, and try to see whether you can print that.

There are some other strange things, when I search osm.org for “SCUOLA DELL’INFANZIA SAN FRANCESCO” I only find https://www.openstreetmap.org/way/547880008, which has the name in lower case. As far as I can see your program does not convert the name to upper case. It is also weird that the ID of that school is not the same as in your list, where it has ID 2272010226. This makes no sense to me.

The school in https://www.openstreetmap.org/way/547880008 (in Pisa) has an addr:street, can you print that ?

hello dear Escada,

many many thanks for the reply - i am glad to hear from you.

thanks for the hint - btw : i do not want to print it out.

i will have a closer look at these sites and cases.

And yes: Many thanks for bringing up “website” as alternative tag.

i will try out this - and will respond all the findings.

greetings
tag the world

hi there i tried this piece of code


{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
    list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

and variants – in other words with and without the following

xpath('tag[@k = "contact:website

run into issues - i try to solve this.

will come back and report all the findings.