LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php simpleXML :parsing an osm-file and storing the output to mysql (https://www.linuxquestions.org/questions/programming-9/php-simplexml-parsing-an-osm-file-and-storing-the-output-to-mysql-4175508017/)

sayhello_to_the_world 06-14-2014 03:30 AM

php simpleXML :parsing an osm-file and storing the output to mysql
 
I am new to PHP's SimpleXML.

The original version of this question was derived from here: 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 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. 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 SimpleXM


PHP Code:

<?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
  ["addr:postcode"~"RM12"]
  (51.5557914,0.2118915,51.5673083,0.2369398)->.point;
  (
  node
  (around.point:100000)
  ["amenity"~"school"];
  way
  (around.point:100000)
  ["amenity"~"school"];
  );
  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 'http://overpass-api.de/api/interpreter';
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) - $startcount($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);
}

?>



how to get more out of it.. at least the adress and the website

where to add those tags in the request`?

in this line:
PHP Code:

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

in advance i thank you for any and all help

TB0ne 06-15-2014 12:05 PM

Quote:

Originally Posted by sayhello_to_the_world (Post 5187978)
I am new to PHP's SimpleXML.
The original version of this question was derived from here: 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 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. 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 SimpleXM how to get more out of it.. at least the adress and the website where to add those tags in the request`?

in this line:
PHP Code:

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


Very similar to this question:
http://www.linuxquestions.org/questi...db-4175506044/

...where you say you wanted to store XML data from OSM into a MySQL database. Which is also the same thread where you said:
Quote:

Originally Posted by sayhello_to_the_world
i do lots of work with PHP

If you do lots of work with PHP, it should be trivial for you to know the answer to what you're after. In your previous thread, you downloaded a Perl program to do what you needed, but never actually seemed to TRY that program. You also said you were going to, and post back your results....which you never did. You posted again about a perl module to also load data into a MySQL database:
http://www.linuxquestions.org/questi...5D-4175507384/

...but again didn't follow up with anything there, either. If you're never going to follow up, post answers to questions, or try what's suggested, why bother posting??? AGAIN, we are always happy to HELP someone, but YOU HAVE TO SHOW EFFORT OF YOUR OWN...yes, I'm sure you'll again say "Wow, great advice, I'll be sure to do it!"...as you have numerous times in the past.

Again, since you've done "lots of work with PHP", did you read the PHP SimpleXML manual???? Look at the examples? Just based on the examples in the manual, anyone with any PHP experience should easily be able to modify what's going on:

http://us2.php.net/manual/en/simplex...ples-basic.php
http://us2.php.net/manual/en/simplex...ples-basic.php

You would very obviously look at the entire record of what's being returned from that query, and add the new field in.


All times are GMT -5. The time now is 10:21 PM.