geocoder.services.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @file
  4. * geocoder.services.inc
  5. */
  6. /**
  7. * Implements hook_services_resources().
  8. */
  9. function geocoder_services_resources() {
  10. return array(
  11. 'geocoder' => array(
  12. 'retrieve' => array(
  13. 'help' => 'Geocode data',
  14. 'file' => array(
  15. 'type' => 'inc',
  16. 'module' => 'geocoder',
  17. 'name' => 'geocoder.services',
  18. ),
  19. 'callback' => 'geocoder_services_geocode',
  20. 'access callback' => 'geocoder_services_access',
  21. 'access arguments append' => TRUE,
  22. 'args' => array(
  23. array(
  24. 'name' => 'handler',
  25. 'type' => 'string',
  26. 'description' => 'The geocoder handler to use - google, gpx, kml etc.',
  27. 'source' => array('path' => '0'),
  28. 'optional' => FALSE,
  29. ),
  30. array(
  31. 'name' => 'data',
  32. 'type' => 'string',
  33. 'description' => 'Value to geocode',
  34. 'source' => array('param' => 'data'),
  35. 'optional' => FALSE,
  36. ),
  37. array(
  38. 'name' => 'output',
  39. 'type' => 'string',
  40. 'description' => 'Output Format (GPX, WKT, etc.)',
  41. 'source' => array('param' => 'output'),
  42. 'optional' => TRUE,
  43. ),
  44. ),
  45. ),
  46. 'index' => array(
  47. 'help' => 'List Geocoder Capabilities',
  48. 'file' => array(
  49. 'type' => 'inc',
  50. 'module' => 'geocoder',
  51. 'name' => 'geocoder.services',
  52. ),
  53. 'callback' => 'geocoder_services_capabilities',
  54. // Always returns TRUE.
  55. 'access callback' => 'geocoder_services_capabilities_ac',
  56. ),
  57. ),
  58. );
  59. }
  60. /**
  61. * Callback.
  62. */
  63. function geocoder_services_capabilities_ac() {
  64. return TRUE;
  65. }
  66. /**
  67. * Callback.
  68. */
  69. function geocoder_services_access($handler, $data, $output) {
  70. if ($handler === 'default') {
  71. $handler = 'json';
  72. }
  73. return geocoder_service_check_perms($handler);
  74. }
  75. /**
  76. * Callback for geocoding service.
  77. *
  78. * @param string $handler
  79. * @param string $data
  80. * @param string $format
  81. *
  82. * @throws \Exception
  83. *
  84. * @return object
  85. */
  86. function geocoder_services_geocode($handler, $data, $format = 'default') {
  87. geophp_load();
  88. geocoder_service_check_request($handler, $format);
  89. $geom = geocoder($handler, $data);
  90. if (!$format || $format === 'default') {
  91. $result = $geom->out('json');
  92. return json_decode($result);
  93. }
  94. return $geom->out($format);
  95. }
  96. /**
  97. * Callback.
  98. */
  99. function geocoder_services_capabilities() {
  100. geophp_load();
  101. $handlers = array();
  102. foreach (geocoder_handler_info() as $hid => $handler) {
  103. $handlers[$hid] = $handler['title'] . ' - ' . $handler['description'];
  104. }
  105. $object = new stdClass();
  106. $object->handlers = $handlers;
  107. $object->output = geoPHP::getAdapterMap();
  108. return $object;
  109. }