Source for file elements.php

Documentation is available at elements.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Openstreetmap
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die();
  11.  
  12. /**
  13.  * Openstreetmap API Elements class for the Joomla Platform
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Openstreetmap
  17.  * @since       13.1
  18.  */
  19. {
  20.     /**
  21.      * Method to create a node
  22.      *
  23.      * @param   integer  $changeset  Changeset id
  24.      * @param   float    $latitude   Latitude of the node
  25.      * @param   float    $longitude  Longitude of the node
  26.      * @param   arary    $tags       Array of tags for a node
  27.      *
  28.      * @return  array  The XML response
  29.      *
  30.      * @since   13.1
  31.      */
  32.     public function createNode($changeset$latitude$longitude$tags)
  33.     {
  34.         $token $this->oauth->getToken();
  35.  
  36.         // Set parameters.
  37.         $parameters array(
  38.             'oauth_token' => $token['key']
  39.         );
  40.  
  41.         // Set the API base
  42.         $base 'node/create';
  43.  
  44.         // Build the request path.
  45.         $path $this->getOption('api.url'$base;
  46.  
  47.         $tag_list '';
  48.  
  49.         // Create XML node
  50.         if (!empty($tags))
  51.         {
  52.             foreach ($tags as $key => $value)
  53.             {
  54.                 $tag_list .= '<tag k="' $key '" v="' $value '"/>';
  55.             }
  56.         }
  57.  
  58.         $xml '<?xml version="1.0" encoding="UTF-8"?>
  59.                 <osm version="0.6" generator="JOpenstreetmap">
  60.                 <node changeset="' $changeset '" lat="' $latitude '" lon="' $longitude '">'
  61.                 . $tag_list .
  62.                 '</node>
  63.                 </osm>';
  64.  
  65.         $header['Content-Type''text/xml';
  66.  
  67.         // Send the request.
  68.         $response $this->oauth->oauthRequest($path'PUT'$parameters$xml$header);
  69.  
  70.         return $response->body;
  71.     }
  72.  
  73.     /**
  74.      * Method to create a way
  75.      *
  76.      * @param   integer  $changeset  Changeset id
  77.      * @param   array    $tags       Array of tags for a way
  78.      * @param   array    $nds        Node ids to refer
  79.      *
  80.      * @return  array   The XML response
  81.      *
  82.      * @since   13.1
  83.      */
  84.     public function createWay($changeset$tags$nds)
  85.     {
  86.         $token $this->oauth->getToken();
  87.  
  88.         // Set parameters.
  89.         $parameters array(
  90.             'oauth_token' => $token['key']
  91.         );
  92.  
  93.         // Set the API base
  94.         $base 'way/create';
  95.  
  96.         // Build the request path.
  97.         $path $this->getOption('api.url'$base;
  98.  
  99.         $tag_list '';
  100.  
  101.         // Create XML node
  102.         if (!empty($tags))
  103.         {
  104.             foreach ($tags as $key => $value)
  105.             {
  106.                 $tag_list .= '<tag k="' $key '" v="' $value '"/>';
  107.             }
  108.         }
  109.  
  110.         $nd_list '';
  111.  
  112.         if (!empty($nds))
  113.         {
  114.             foreach ($nds as $value)
  115.             {
  116.                 $nd_list .= '<nd ref="' $value '"/>';
  117.             }
  118.         }
  119.  
  120.         $xml '<?xml version="1.0" encoding="UTF-8"?>
  121.                 <osm version="0.6" generator="JOpenstreetmap">
  122.                 <way changeset="' $changeset '">'
  123.                     . $tag_list
  124.                     . $nd_list .
  125.                 '</way>
  126.             </osm>';
  127.  
  128.         $header['Content-Type''text/xml';
  129.  
  130.         // Send the request.
  131.         $response $this->oauth->oauthRequest($path'PUT'$parameters$xml$header);
  132.  
  133.         return $response->body;
  134.     }
  135.  
  136.     /**
  137.      * Method to create a relation
  138.      *
  139.      * @param   integer  $changeset  Changeset id
  140.      * @param   array    $tags       Array of tags for a relation
  141.      * @param   array    $members    Array of members for a relation
  142.      *                                eg: $members = array(array("type"=>"node", "role"=>"stop", "ref"=>"123"), array("type"=>"way", "ref"=>"123"))
  143.      *
  144.      * @return  array  The XML response
  145.      *
  146.      * @since   13.1
  147.      */
  148.     public function createRelation($changeset$tags$members)
  149.     {
  150.         $token $this->oauth->getToken();
  151.  
  152.         // Set parameters.
  153.         $parameters array(
  154.             'oauth_token' => $token['key']
  155.         );
  156.  
  157.         // Set the API base
  158.         $base 'relation/create';
  159.  
  160.         // Build the request path.
  161.         $path $this->getOption('api.url'$base;
  162.  
  163.         $tag_list '';
  164.  
  165.         // Create XML node
  166.         if (!empty($tags))
  167.         {
  168.             foreach ($tags as $key => $value)
  169.             {
  170.                 $tag_list .= '<tag k="' $key '" v="' $value '"/>';
  171.             }
  172.         }
  173.  
  174.         // Members
  175.         $member_list '';
  176.  
  177.         if (!empty($members))
  178.         {
  179.             foreach ($members as $member)
  180.             {
  181.                 if ($member['type'== "node")
  182.                 {
  183.                     $member_list .= '<member type="' $member['type''" role="' $member['role''" ref="' $member['ref''"/>';
  184.                 }
  185.                 elseif ($member['type'== "way")
  186.                 {
  187.                     $member_list .= '<member type="' $member['type''" ref="' $member['ref''"/>';
  188.                 }
  189.             }
  190.         }
  191.  
  192.         $xml '<?xml version="1.0" encoding="UTF-8"?>
  193.                 <osm version="0.6" generator="JOpenstreetmap">
  194.                 <relation relation="' $changeset '" >'
  195.                     . $tag_list
  196.                     . $member_list .
  197.                 '</relation>
  198.             </osm>';
  199.  
  200.         $header['Content-Type''text/xml';
  201.  
  202.         // Send the request.
  203.         $response $this->oauth->oauthRequest($path'PUT'$parameters$xml$header);
  204.  
  205.         return $response->body;
  206.     }
  207.  
  208.     /**
  209.      * Method to read an element [node|way|relation]
  210.      *
  211.      * @param   string   $element  [node|way|relation]
  212.      * @param   integer  $id       Element identifier
  213.      *
  214.      * @return  array  The XML response
  215.      *
  216.      * @since   13.1
  217.      * @throws  DomainException
  218.      */
  219.     public function readElement($element$id)
  220.     {
  221.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  222.         {
  223.             throw new DomainException("Element should be a node, a way or a relation");
  224.         }
  225.  
  226.         // Set the API base
  227.         $base $element '/' $id;
  228.  
  229.         // Build the request path.
  230.         $path $this->getOption('api.url'$base;
  231.  
  232.         // Send the request.
  233.         $xml_string $this->sendRequest($path);
  234.  
  235.         return $xml_string->$element;
  236.     }
  237.  
  238.     /**
  239.      * Method to update an Element [node|way|relation]
  240.      *
  241.      * @param   string   $element  [node|way|relation]
  242.      * @param   string   $xml      Full reperentation of the element with a version number
  243.      * @param   integer  $id       Element identifier
  244.      *
  245.      * @return  array   The xml response
  246.      *
  247.      * @since   13.1
  248.      * @throws  DomainException
  249.      */
  250.     public function updateElement($element$xml$id)
  251.     {
  252.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  253.         {
  254.             throw new DomainException("Element should be a node, a way or a relation");
  255.         }
  256.  
  257.         $token $this->oauth->getToken();
  258.  
  259.         // Set parameters.
  260.         $parameters array(
  261.             'oauth_token' => $token['key']
  262.         );
  263.  
  264.         // Set the API base
  265.         $base $element '/' $id;
  266.  
  267.         // Build the request path.
  268.         $path $this->getOption('api.url'$base;
  269.  
  270.         $header['Content-Type''text/xml';
  271.  
  272.         // Send the request.
  273.         $response $this->oauth->oauthRequest($path'PUT'$parameters$xml$header);
  274.  
  275.         return $response->body;
  276.     }
  277.  
  278.     /**
  279.      * Method to delete an element [node|way|relation]
  280.      *
  281.      * @param   string   $element    [node|way|relation]
  282.      * @param   integer  $id         Element identifier
  283.      * @param   integer  $version    Element version
  284.      * @param   integer  $changeset  Changeset identifier
  285.      * @param   float    $latitude   Latitude of the element
  286.      * @param   float    $longitude  Longitude of the element
  287.      *
  288.      * @return  array   The XML response
  289.      *
  290.      * @since   13.1
  291.      * @throws  DomainException
  292.      */
  293.     public function deleteElement($element$id$version$changeset$latitude null$longitude null)
  294.     {
  295.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  296.         {
  297.             throw new DomainException("Element should be a node, a way or a relation");
  298.         }
  299.  
  300.         $token $this->oauth->getToken();
  301.  
  302.         // Set parameters.
  303.         $parameters array(
  304.             'oauth_token' => $token['key']
  305.         );
  306.  
  307.         // Set the API base
  308.         $base $element '/' $id;
  309.  
  310.         // Build the request path.
  311.         $path $this->getOption('api.url'$base;
  312.  
  313.         // Create xml
  314.         $xml '<?xml version="1.0" encoding="UTF-8"?>
  315.                 <osm version="0.6" generator="JOpenstreetmap">
  316.                 <' $element ' id="' $id '" version="' $version '" changeset="' $changeset '"';
  317.  
  318.         if (!empty($latitude&& !empty($longitude))
  319.         {
  320.             $xml .= ' lat="' $latitude '" lon="' $longitude '"';
  321.         }
  322.  
  323.         $xml .= '/></osm>';
  324.  
  325.         $header['Content-Type''text/xml';
  326.  
  327.         // Send the request.
  328.         $response $this->oauth->oauthRequest($path'DELETE'$parameters$xml$header);
  329.  
  330.         return $response->body;
  331.     }
  332.  
  333.     /**
  334.      * Method to get history of an element [node|way|relation]
  335.      *
  336.      * @param   string   $element  [node|way|relation]
  337.      * @param   integer  $id       Element identifier
  338.      *
  339.      * @return  array   The XML response
  340.      *
  341.      * @since   13.1
  342.      * @throws  DomainException
  343.      */
  344.     public function historyOfElement($element$id)
  345.     {
  346.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  347.         {
  348.             throw new DomainException("Element should be a node, a way or a relation");
  349.         }
  350.  
  351.         // Set the API base
  352.         $base $element '/' $id '/history';
  353.  
  354.         // Build the request path.
  355.         $path $this->getOption('api.url'$base;
  356.  
  357.         // Send the request.
  358.         $xml_string $this->sendRequest($path);
  359.  
  360.         return $xml_string->$element;
  361.     }
  362.  
  363.     /**
  364.      * Method to get details about a version of an element [node|way|relation]
  365.      *
  366.      * @param   string   $element  [node|way|relation]
  367.      * @param   integer  $id       Element identifier
  368.      * @param   integer  $version  Element version
  369.      *
  370.      * @return  array    The XML response
  371.      *
  372.      * @since   13.1
  373.      * @throws  DomainException
  374.      */
  375.     public function versionOfElement($element$id ,$version)
  376.     {
  377.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  378.         {
  379.             throw new DomainException("Element should be a node, a way or a relation");
  380.         }
  381.  
  382.         // Set the API base
  383.         $base $element '/' $id '/' $version;
  384.  
  385.         // Build the request path.
  386.         $path $this->getOption('api.url'$base;
  387.  
  388.         // Send the request.
  389.         $xml_string $this->sendRequest($path);
  390.  
  391.         return $xml_string->$element;
  392.     }
  393.  
  394.     /**
  395.      * Method to get data about multiple ids of an element [node|way|relation]
  396.      *
  397.      * @param   string  $element  [nodes|ways|relations] - use plural word
  398.      * @param   string  $params   Comma separated list of ids belonging to type $element
  399.      *
  400.      * @return  array   The XML response
  401.      *
  402.      * @since   13.1
  403.      * @throws  DomainException
  404.      */
  405.     public function multiFetchElements($element$params)
  406.     {
  407.         if ($element != 'nodes' && $element != 'ways' && $element != 'relations')
  408.         {
  409.             throw new DomainException("Element should be nodes, ways or relations");
  410.         }
  411.  
  412.         // Get singular word
  413.         $single_element substr($element0strlen($element1);
  414.  
  415.         // Set the API base, $params is a string with comma seperated values
  416.         $base $element '?' $element "=" $params;
  417.  
  418.         // Build the request path.
  419.         $path $this->getOption('api.url'$base;
  420.  
  421.         // Send the request.
  422.         $xml_string $this->sendRequest($path);
  423.  
  424.         return $xml_string->$single_element;
  425.     }
  426.  
  427.     /**
  428.      * Method to get relations for an Element [node|way|relation]
  429.      *
  430.      * @param   string   $element  [node|way|relation]
  431.      * @param   integer  $id       Element identifier
  432.      *
  433.      * @return  array   The XML response
  434.      *
  435.      * @since   13.1
  436.      * @throws  DomainException
  437.      */
  438.     public function relationsForElement($element$id)
  439.     {
  440.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  441.         {
  442.             throw new DomainException("Element should be a node, a way or a relation");
  443.         }
  444.  
  445.         // Set the API base
  446.         $base $element '/' $id '/relations';
  447.  
  448.         // Build the request path.
  449.         $path $this->getOption('api.url'$base;
  450.  
  451.         // Send the request.
  452.         $xml_string $this->sendRequest($path);
  453.  
  454.         return $xml_string->$element;
  455.     }
  456.  
  457.     /**
  458.      * Method to get ways for a Node element
  459.      *
  460.      * @param   integer  $id  Node identifier
  461.      *
  462.      * @return  array  The XML response
  463.      *
  464.      * @since   13.1
  465.      */
  466.     public function waysForNode($id)
  467.     {
  468.         // Set the API base
  469.         $base 'node/' $id '/ways';
  470.  
  471.         // Build the request path.
  472.         $path $this->getOption('api.url'$base;
  473.  
  474.         // Send the request.
  475.         $xml_string $this->sendRequest($path);
  476.  
  477.         return $xml_string->way;
  478.     }
  479.  
  480.     /**
  481.      * Method to get full information about an element [way|relation]
  482.      *
  483.      * @param   string   $element  [way|relation]
  484.      * @param   integer  $id       Identifier
  485.      *
  486.      * @return  array  The XML response
  487.      *
  488.      * @since   13.1
  489.      * @throws  DomainException
  490.      */
  491.     public function fullElement($element$id)
  492.     {
  493.         if ($element != 'way' && $element != 'relation')
  494.         {
  495.             throw new DomainException("Element should be a way or a relation");
  496.         }
  497.  
  498.         // Set the API base
  499.         $base $element '/' $id '/full';
  500.  
  501.         // Build the request path.
  502.         $path $this->getOption('api.url'$base;
  503.  
  504.         // Send the request.
  505.         $xml_string $this->sendRequest($path);
  506.  
  507.         return $xml_string->node;
  508.     }
  509.  
  510.     /**
  511.      * Method used by the DWG to hide old versions of elements containing data privacy or copyright infringements
  512.      *
  513.      * @param   string   $element       [node|way|relation]
  514.      * @param   integer  $id            Element identifier
  515.      * @param   integer  $version       Element version
  516.      * @param   integer  $redaction_id  Redaction id
  517.      *
  518.      * @return  array   The xml response
  519.      *
  520.      * @since   13.1
  521.      * @throws  DomainException
  522.      */
  523.     public function redaction($element$id$version$redaction_id)
  524.     {
  525.         if ($element != 'node' && $element != 'way' && $element != 'relation')
  526.         {
  527.             throw new DomainException("Element should be a node, a way or a relation");
  528.         }
  529.  
  530.         $token $this->oauth->getToken();
  531.  
  532.         // Set parameters.
  533.         $parameters array(
  534.             'oauth_token' => $token['key']
  535.         );
  536.  
  537.         // Set the API base
  538.         $base $element '/' $id '/' $version '/redact?redaction=' $redaction_id;
  539.  
  540.         // Build the request path.
  541.         $path $this->getOption('api.url'$base;
  542.  
  543.         // Send the request.
  544.         $response $this->oauth->oauthRequest($path'PUT'$parameters);
  545.  
  546.         $xml_string simplexml_load_string($response->body);
  547.  
  548.         return $xml_string;
  549.     }
  550. }

Documentation generated on Tue, 19 Nov 2013 15:02:24 +0100 by phpDocumentor 1.4.3