Server.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * GetCapabilities
  4. * DescribeProcess
  5. * Execute
  6. */
  7. Lib::loadClass('Api_WpsV1_ServerBase');
  8. Lib::loadClass('Api_WfsException');
  9. Lib::loadClass('Api_WfsGeomTypeConverter');
  10. Lib::loadClass('Api_WfsNs');
  11. Lib::loadClass('Core_XmlWriter');
  12. Lib::loadClass('DBG');
  13. Lib::loadClass('Api_WpsV1_GetCapabilities');
  14. Lib::loadClass('Api_WpsV1_DescribeProcess');
  15. Lib::loadClass('Core_AclBase');
  16. Lib::loadClass('Type_ApiRequest');
  17. Lib::loadClass('Type_WpsProcess');
  18. class Api_WpsV1_Server extends Api_WpsV1_ServerBase {
  19. public function run(Type_ApiRequest $request) {
  20. $document = '';
  21. if ('WPS' != V::geti('service', '', $request->query)) {
  22. throw new Api_WfsException("Only WPS Service is allowed");
  23. }
  24. $req = V::geti('request', '', $request->query);
  25. if (!empty($req)) {
  26. $methodName = "{$req}Action";
  27. if (!method_exists($this, $methodName)) {
  28. throw new Api_WfsException("Not Implemented " . htmlspecialchars($req), 501);
  29. }
  30. $this->DBG("WpsServer->{$methodName}() ...", __LINE__);
  31. $document = $this->$methodName($urlQuery);
  32. }
  33. else {
  34. $this->DBG("WpsServer->parseXMLRequest() ...", __LINE__);
  35. $document = $this->parseXMLRequest();
  36. header('Content-type: application/xml');
  37. echo '<?xml version="1.0" encoding="UTF-8"?>';
  38. echo $document; exit;// TODO: return $document;
  39. }
  40. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">$document (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($document);echo'</pre>';}
  41. if ('raw' == V::get('outputFormat', '', $request->query)) {
  42. header('Content-type: text/plain; charset=utf-8');
  43. echo $document;
  44. } else {
  45. header('Content-type: application/xml');
  46. echo $document;
  47. }
  48. }
  49. public function parseXMLRequest() {
  50. $data = array();
  51. $reqContent = Request::getRequestBody();
  52. if (empty($reqContent)) {
  53. throw new Exception("Empty request");
  54. }
  55. $parserXml = xml_parser_create();
  56. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  57. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  58. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  59. throw new Exception("Error parsing xml");
  60. }
  61. xml_parser_free($parserXml);
  62. if (empty($tags)) {
  63. throw new Exception("Empty structure from request");
  64. }
  65. $rootTagName = V::get('tag', '', $tags[0]);
  66. if ('Transaction' == $rootTagName) return $this->_parseTransactionXmlStruct($reqContent, $tags);
  67. throw new Api_WfsException("Not implemented '{$rootTagName}' #L." . __LINE__, 501);
  68. }
  69. function describeProcessAction() {
  70. $identifier = V::geti('identifier', '', $_REQUEST);
  71. if (empty($identifier)) throw new HttpException("Missing identifier", 400);
  72. if ('JTS:Aarea' === $identifier) return self::exampleDescribeProcess_JTS_Area();
  73. if ('vec:Feature' === $identifier) return self::exampleDescribeProcess_vec_Feature();
  74. $wpsServerUrl = $this->getBaseUri();
  75. $wpsProcess = $this->_getWpsProcess($identifier); // TODO: get from WPS Process Factory/Helper
  76. if (!$wpsProcess) throw new Api_WfsException("Forbidden", 403);
  77. Api_WpsV1_DescribeProcess::sendDescribeProcessXml($wpsServerUrl, $wpsProcess);
  78. }
  79. function _getWpsProcess($identifier) {
  80. $wpsProcess = Type_WpsProcess::build([
  81. 'identifier' => $identifier,
  82. 'title' => "Title({$identifier})",
  83. 'description' => "Desc($identifier)",
  84. 'maxOccurs' => "1",
  85. 'minOccurs' => "1",
  86. 'dataInputs' => [
  87. [
  88. 'identifier' => $identifier,
  89. 'title' => $title,
  90. 'description' => $description,
  91. 'type' => 'literal',
  92. ],
  93. ],
  94. // 'MimeType' => text/xml, 'Encoding' => base64, 'Schema' => http://foo.bar/gml/3.1.0/polygon.xsd
  95. 'defaultOutput' => [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ],
  96. 'supportedOutput' => [
  97. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ],
  98. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.1" ],
  99. [ 'MimeType' => "application/json" ],
  100. // [ 'MimeType' => "application/wfs-collection-1.0" ],
  101. // [ 'MimeType' => "application/wfs-collection-1.1" ],
  102. // [ 'MimeType' => "application/zip" ],
  103. ]
  104. ]);
  105. return $wpsProcess;
  106. }
  107. static function exampleDescribeProcess_JTS_Area() {
  108. header('Content-type: application/xml; charset=utf-8');
  109. echo '<?xml version="1.0" encoding="UTF-8"?><wps:ProcessDescriptions xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en" service="WPS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd"><ProcessDescription wps:processVersion="1.0.0" statusSupported="true" storeSupported="true"><ows:Identifier>JTS:area</ows:Identifier><ows:Title>Area</ows:Title><ows:Abstract>Returns the area of a geometry, in the units of the geometry. Assumes a Cartesian plane, so this process is only recommended for non-geographic CRSes.</ows:Abstract><DataInputs><Input maxOccurs="1" minOccurs="1"><ows:Identifier>geom</ows:Identifier><ows:Title>geom</ows:Title><ows:Abstract>Input geometry</ows:Abstract><ComplexData><Default><Format><MimeType>text/xml; subtype=gml/3.1.1</MimeType></Format></Default><Supported><Format><MimeType>text/xml; subtype=gml/3.1.1</MimeType></Format><Format><MimeType>text/xml; subtype=gml/2.1.2</MimeType></Format><Format><MimeType>application/wkt</MimeType></Format><Format><MimeType>application/json</MimeType></Format><Format><MimeType>application/gml-3.1.1</MimeType></Format><Format><MimeType>application/gml-2.1.2</MimeType></Format></Supported></ComplexData></Input></DataInputs><ProcessOutputs><Output><ows:Identifier>result</ows:Identifier><ows:Title>result</ows:Title><LiteralOutput><ows:DataType>double</ows:DataType></LiteralOutput></Output></ProcessOutputs></ProcessDescription></wps:ProcessDescriptions>';
  110. exit;
  111. }
  112. static function exampleDescribeProcess_vec_Feature() {
  113. header('Content-type: application/xml; charset=utf-8');
  114. echo '<?xml version="1.0" encoding="UTF-8"?><wps:ProcessDescriptions xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en" service="WPS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd"><ProcessDescription wps:processVersion="1.0.0" statusSupported="true" storeSupported="true"><ows:Identifier>vec:Feature</ows:Identifier><ows:Title>Feature from Geometry</ows:Title><ows:Abstract>Converts a geometry into a feature collection.</ows:Abstract><DataInputs><Input maxOccurs="1" minOccurs="1"><ows:Identifier>geometry</ows:Identifier><ows:Title>geometry</ows:Title><ows:Abstract>Input geometry</ows:Abstract><ComplexData><Default><Format><MimeType>text/xml; subtype=gml/3.1.1</MimeType></Format></Default><Supported><Format><MimeType>text/xml; subtype=gml/3.1.1</MimeType></Format><Format><MimeType>text/xml; subtype=gml/2.1.2</MimeType></Format><Format><MimeType>application/wkt</MimeType></Format><Format><MimeType>application/json</MimeType></Format><Format><MimeType>application/gml-3.1.1</MimeType></Format><Format><MimeType>application/gml-2.1.2</MimeType></Format></Supported></ComplexData></Input><Input maxOccurs="1" minOccurs="1"><ows:Identifier>crs</ows:Identifier><ows:Title>crs</ows:Title><ows:Abstract>Coordinate reference system of the input geometry (if not provided in the geometry)</ows:Abstract><LiteralData><ows:AnyValue/></LiteralData></Input><Input maxOccurs="1" minOccurs="1"><ows:Identifier>typeName</ows:Identifier><ows:Title>typeName</ows:Title><ows:Abstract>Feauturetype name for the feature collection</ows:Abstract><LiteralData><ows:AnyValue/></LiteralData></Input></DataInputs><ProcessOutputs><Output><ows:Identifier>result</ows:Identifier><ows:Title>result</ows:Title><ComplexOutput><Default><Format><MimeType>text/xml; subtype=wfs-collection/1.0</MimeType></Format></Default><Supported><Format><MimeType>text/xml; subtype=wfs-collection/1.0</MimeType></Format><Format><MimeType>text/xml; subtype=wfs-collection/1.1</MimeType></Format><Format><MimeType>application/json</MimeType></Format><Format><MimeType>application/wfs-collection-1.0</MimeType></Format><Format><MimeType>application/wfs-collection-1.1</MimeType></Format><Format><MimeType>application/zip</MimeType></Format></Supported></ComplexOutput></Output></ProcessOutputs></ProcessDescription></wps:ProcessDescriptions>';
  115. exit;
  116. }
  117. public function getCapabilitiesAction() {
  118. $wpsServerUrl = $this->getBaseUri();
  119. $serviceTitle = "Web Processing Service";
  120. $serviceDescription = "Web Processing Service";
  121. $idDefaultDB = DB::getPDO()->getZasobId();
  122. $aclList = array_filter($this->_usrAcl->getTablesAcl(), function ($acl) use ($idDefaultDB) {
  123. // // $dataSourceName = 'default_db';// TODO: getSourceName
  124. // // $tblName = $tblAcl->getName();
  125. // // try {
  126. // // $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  127. // // } catch (Exception $e) {
  128. // // // echo "Error for table({$tblName}): " . $e->getMessage() . "\n";
  129. // // }
  130. // // if (!$acl) {
  131. // // // TODO: error log msg
  132. // // return false;
  133. // // }
  134. return ($acl instanceof Core_AclBase && $idDefaultDB == $acl->getDB()); // hide non default_db tables
  135. });
  136. switch (V::get('outputFormat', 'xml', $_GET)) {
  137. case 'csv': {
  138. Api_WpsV1_GetCapabilities::sendGetCapabilitiesCsv($wpsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  139. } break;
  140. case 'xml': {
  141. Api_WpsV1_GetCapabilities::sendGetCapabilitiesXml($wpsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  142. } break;
  143. default: throw new Api_WfsException("Not Implemented outputFormat", 501); // , null, 'NotImplemented', 'request');
  144. }
  145. exit;
  146. }
  147. }