LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2014, 03:30 AM   #1
sayhello_to_the_world
Member
 
Registered: May 2013
Posts: 229

Rep: Reputation: Disabled
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

Last edited by sayhello_to_the_world; 06-14-2014 at 03:32 AM.
 
Old 06-15-2014, 12:05 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by sayhello_to_the_world View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP parsing file noony123 Programming 2 07-25-2012 10:32 PM
Parsing tcpdump output file Edward18 Linux - Networking 2 01-02-2012 04:58 AM
[SOLVED] Parsing output; End of file option? CodyK479 Linux - Newbie 3 02-23-2011 09:13 PM
Apache PHP 5 simplexml is not loading dcinadr Linux - Software 4 01-10-2011 12:07 AM
LXer: SimpleXML processing with PHP LXer Syndicated Linux News 0 10-10-2006 10:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration