$proj25833 = new Proj4phpProj('EPSG:25833', $proj4); $proj31468 = new Proj4phpProj('EPSG:31468', $proj4); // GPS // latitude longitude // 48,831938 2,355781 // 48°49'54.977'' 2°21'20.812'' // // L93 // 652709.401 6859290.946 // // LI // 601413.709 1125717.730 // $pointSrc = new proj4phpPoint('652709.401', '6859290.946'); echo "Source : " . $pointSrc->toShortString() . " in L93 <br>"; $pointDest = $proj4->transform($projL93, $projWGS84, $pointSrc); echo "Conversion : " . $pointDest->toShortString() . " in WGS84<br><br>"; $pointSrc = $pointDest; echo "Source : " . $pointSrc->toShortString() . " in WGS84<br>"; $pointDest = $proj4->transform($projWGS84, $projLSud, $pointSrc); echo "Conversion : " . $pointDest->toShortString() . " in Lambert Sud<br><br>"; $pointSrc = $pointDest; echo "Source : " . $pointSrc->toShortString() . " in Lambert Sud<br>"; $pointDest = $proj4->transform($projLSud, $projWGS84, $pointSrc); echo "Conversion : " . $pointDest->toShortString() . " in WGS84<br><br>"; $pointSrc = $pointDest; echo "Source : " . $pointSrc->toShortString() . " in WGS84<br>"; $pointDest = $proj4->transform($projWGS84, $projLI, $pointSrc); echo "Conversion : " . $pointDest->toShortString() . " in LI <br><br>"; $pointSrc = $pointDest; echo "Source : " . $pointSrc->toShortString() . " in LI<br>";
public function readData($source_definition, $rest_parameters = array()) { // It may take a while for the SHP to be read set_time_limit(0); // Get the limit and offset list($limit, $offset) = Pager::calculateLimitAndOffset(); // Disregard the paging when rest parameters are given if (!empty($rest_parameters)) { $limit = PHP_INT_MAX; $offset = 0; } $uri = $source_definition['uri']; $columns = array(); $epsg = $source_definition['epsg']; // The tmp folder of the system, if none is given // abort the process $tmp_path = sys_get_temp_dir(); if (empty($tmp_path)) { // If this occurs then the server is not configured correctly, thus a 500 error is thrown \App::abort(500, "The temp directory, retrieved by the operating system, could not be retrieved."); } // Get the columns $columns = $this->tabular_columns->getColumnAliases($source_definition['id'], 'ShpDefinition'); // Get the geo properties $geo_properties = $this->geo_property->getGeoProperties($source_definition['id'], 'ShpDefinition'); $geo = array(); foreach ($geo_properties as $geo_prop) { $geo[$geo_prop['property']] = $geo_prop['path']; } if (!$columns) { \App::abort(500, "Cannot find the columns of the SHP definition."); } try { // Create the array in which all the resulting objects will be placed $arrayOfRowObjects = array(); // Prepare the options to read the SHP file $options = array('noparts' => false); $is_url = substr($uri, 0, 4) == "http"; // If the shape files are located on an HTTP address, fetch them and store them locally if ($is_url) { $tmp_file_name = uniqid(); $tmp_file = $tmp_path . "/" . $tmp_file_name; file_put_contents($tmp_file . ".shp", file_get_contents(substr($uri, 0, strlen($uri) - 4) . ".shp")); file_put_contents($tmp_file . ".dbf", file_get_contents(substr($uri, 0, strlen($uri) - 4) . ".dbf")); file_put_contents($tmp_file . ".shx", file_get_contents(substr($uri, 0, strlen($uri) - 4) . ".shx")); // Along this file the class will use file.shx and file.dbf $shp = new ShapeReader($tmp_file . ".shp", $options); } else { $shp = new ShapeReader($uri, $options); // along this file the class will use file.shx and file.dbf } // Keep track of the total amount of rows $total_rows = 0; // Get the shape records in the binary file while ($record = $shp->getNext()) { if ($offset <= $total_rows && $offset + $limit > $total_rows) { // Every shape record is parsed as an anonymous object with the properties attached to it $rowobject = new \stdClass(); // Get the dBASE data $dbf_data = $record->getDbfData(); foreach ($dbf_data as $property => $value) { $property_alias = $columns[$property]; $property = trim($property); $property_alias = $columns[$property]; $rowobject->{$property_alias} = trim($value); } // Read the shape data $shp_data = $record->getShpData(); if (!empty($epsg)) { $proj4 = new \Proj4php(); $projSrc = new \Proj4phpProj('EPSG:' . $epsg, $proj4); $projDest = new \Proj4phpProj('EPSG:4326', $proj4); } // It it's not a point, it's a collection of coordinates describing a shape if (!empty($shp_data['parts'])) { $parts = array(); foreach ($shp_data['parts'] as $part) { $points = array(); foreach ($part['points'] as $point) { $x = $point['x']; $y = $point['y']; // Translate the coordinates to WSG84 geo coordinates if (!empty($epsg)) { $pointSrc = new \proj4phpPoint($x, $y); $pointDest = $proj4->transform($projSrc, $projDest, $pointSrc); $x = $pointDest->x; $y = $pointDest->y; } $points[] = $x . ',' . $y; } array_push($parts, implode(" ", $points)); } // Parts only contains 1 shape, thus 1 geo entry $alias = reset($geo); $rowobject->{$alias} = implode(';', $parts); } if (isset($shp_data['x'])) { $x = $shp_data['x']; $y = $shp_data['y']; if (!empty($epsg)) { $pointSrc = new \proj4phpPoint($x, $y); $pointDest = $proj4->transform($projSrc, $projDest, $pointSrc); $x = $pointDest->x; $y = $pointDest->y; } $rowobject->{$geo}['longitude'] = $x; $rowobject->{$geo}['latitude'] = $y; } array_push($arrayOfRowObjects, $rowobject); } $total_rows++; } // Calculate the paging headers properties $paging = Pager::calculatePagingHeaders($limit, $offset, $total_rows); $data_result = new Data(); $data_result->data = $arrayOfRowObjects; $data_result->geo = $geo; $data_result->paging = $paging; $data_result->preferred_formats = array('map'); return $data_result; } catch (Exception $ex) { \App::abort(500, "Something went wrong while putting the SHP files in a temporary directory or during the extraction of the SHP data. The error message is: {$ex->getMessage}()."); } }
} if ($error === true) { if ($format == 'json') { echo "{\"status\":\"error\", \"erreur\": {\"code\": 2, \"message\": \"Wrong parameters.\"} }"; exit; } else { echo "<reponse>"; echo " <erreur>"; echo " <code>2</code>"; echo " <message>Wrong parameters</message>"; echo " </erreur>"; echo "</reponse>"; exit; } } $pointSrc = new proj4phpPoint($x, $y); $pointDest = $proj4->transform($projsource, $projdest, $pointSrc); $tgtProjection = str_replace(':', '::', $tgtProjection); if ($format == 'json') { echo "{\"status\" :\"success\", \"point\" : {\"x\":" . $pointDest->x . ", \"y\":" . $pointDest->y . ",\"projection\" :\"" . $tgtProjection . "\"}}"; exit; } else { header("Content-Type:text/xml"); echo "<reponse>"; echo "<point>"; echo "<x>" . $pointDest->x . "</x>"; echo "<y>" . $pointDest->y . "</y>"; echo "<projection>" . $tgtProjection . "</projection>"; echo "</point>"; echo "</reponse>"; }
/** * Get layer tile info. * */ public static function getLayerTileInfo($layerName, $project, $wms_xml, $tileMatrixSetList) { $DOTS_PER_INCH = 72; $METERS_PER_INCH = 0.0254000508001016; $INCHES_PER_UNIT = array('inches' => 1.0, 'ft' => 12.0, 'mi' => 63360.0, 'm' => 39.37, 'km' => 39370, 'dd' => 4374754, 'yd' => 36); $INCHES_PER_UNIT["in"] = $INCHES_PER_UNIT['inches']; $INCHES_PER_UNIT["degrees"] = $INCHES_PER_UNIT['dd']; $INCHES_PER_UNIT["nmi"] = 1852 * $INCHES_PER_UNIT['m']; $tileWidth = 256.0; $tileHeight = 256.0; $rootLayer = $wms_xml->xpath("//wms:Capability/wms:Layer"); if (!$rootLayer || count($rootLayer) == 0) { return null; } $rootLayer = $rootLayer[0]; $rootExtent = array((double) $rootLayer->EX_GeographicBoundingBox->westBoundLongitude, (double) $rootLayer->EX_GeographicBoundingBox->southBoundLatitude, (double) $rootLayer->EX_GeographicBoundingBox->eastBoundLongitude, (double) $rootLayer->EX_GeographicBoundingBox->northBoundLatitude); $opt = $project->getOptions(); $scales = array_merge(array(), $opt->mapScales); rsort($scales); $layers = $project->getLayers(); $layer = $layers->{$layerName}; $xmlLayer = $wms_xml->xpath('//wms:Layer/wms:Name[. ="' . $layer->name . '"]/parent::*'); if (!$rootLayer || count($rootLayer) == 0) { return null; } $xmlLayer = $xmlLayer[0]; $layerExtent = null; $xmlLayers = $wms_xml->xpath('//wms:Layer/wms:Name[. ="' . $layer->name . '"]/parent::*//wms:Layer'); foreach ($xmlLayers as $xmlcLayer) { if (!property_exists($xmlcLayer, 'Layer')) { if ($layerExtent == null) { $layerExtent = array((double) $xmlcLayer->EX_GeographicBoundingBox->westBoundLongitude, (double) $xmlcLayer->EX_GeographicBoundingBox->southBoundLatitude, (double) $xmlcLayer->EX_GeographicBoundingBox->eastBoundLongitude, (double) $xmlcLayer->EX_GeographicBoundingBox->northBoundLatitude); } else { if ($layerExtent[0] > (double) $xmlcLayer->EX_GeographicBoundingBox->westBoundLongitude) { $layerExtent[0] = (double) $xmlcLayer->EX_GeographicBoundingBox->westBoundLongitude; } if ($layerExtent[1] > (double) $xmlcLayer->EX_GeographicBoundingBox->southBoundLatitude) { $layerExtent[1] = (double) $xmlcLayer->EX_GeographicBoundingBox->southBoundLatitude; } if ($layerExtent[2] < (double) $xmlcLayer->EX_GeographicBoundingBox->eastBoundLongitude) { $layerExtent[2] = (double) $xmlcLayer->EX_GeographicBoundingBox->eastBoundLongitude; } if ($layerExtent[3] < (double) $xmlcLayer->EX_GeographicBoundingBox->northBoundLatitude) { $layerExtent[3] = (double) $xmlcLayer->EX_GeographicBoundingBox->northBoundLatitude; } } } } if ($layerExtent == null) { $layerExtent = array((double) $xmlLayer->EX_GeographicBoundingBox->westBoundLongitude, (double) $xmlLayer->EX_GeographicBoundingBox->southBoundLatitude, (double) $xmlLayer->EX_GeographicBoundingBox->eastBoundLongitude, (double) $xmlLayer->EX_GeographicBoundingBox->northBoundLatitude); } // cannot be extra rootExtent if ($layerExtent[0] < $rootExtent[0]) { $layerExtent[0] = $rootExtent[0]; } if ($layerExtent[1] < $rootExtent[1]) { $layerExtent[1] = $rootExtent[1]; } if ($layerExtent[2] > $rootExtent[2]) { $layerExtent[2] = $rootExtent[2]; } if ($layerExtent[3] > $rootExtent[3]) { $layerExtent[3] = $rootExtent[3]; } $lowerCorner = (object) array('x' => $layerExtent[0], 'y' => $layerExtent[1]); $upperCorner = (object) array('x' => $layerExtent[2], 'y' => $layerExtent[3]); $opt = $project->getOptions(); jClasses::inc("proj4php~proj4php"); $proj4 = new Proj4php(); Proj4php::$defs[$opt->projection->ref] = $opt->projection->proj4; $sourceProj = new Proj4phpProj('EPSG:4326', $proj4); $tileMatrixSetLinkList = array(); foreach ($tileMatrixSetList as $tileMatrixSet) { $destProj = new Proj4phpProj($tileMatrixSet->ref, $proj4); $sourceMinPt = new proj4phpPoint($layerExtent[0], $layerExtent[1]); $destMinPt = $proj4->transform($sourceProj, $destProj, $sourceMinPt); $sourceMaxPt = new proj4phpPoint($layerExtent[2], $layerExtent[3]); $destMaxPt = $proj4->transform($sourceProj, $destProj, $sourceMaxPt); $extent = array($destMinPt->x, $destMinPt->y, $destMaxPt->x, $destMaxPt->y); $tileMatrixList = $tileMatrixSet->tileMatrixList; $tileMatrixLimits = array(); foreach ($tileMatrixList as $k => $tileMatrix) { $maxScale = $layer->maxScale; /* if ( $maxScale > $scales[0] ) $maxScale = $scales[0]; * */ $minScale = $layer->minScale; /* if ( $minScale < $scales[ count($scales) - 1 ] ) $minScale = $scales[ count($scales) - 1 ]; * */ if ($tileMatrix->scaleDenominator <= $maxScale && $tileMatrix->scaleDenominator >= $minScale) { $res = $tileMatrix->resolution; $minCol = floor(($extent[0] - $tileMatrix->left) / ($tileWidth * $res)); $maxCol = floor(($extent[2] - $tileMatrix->left) / ($tileWidth * $res)); $minRow = floor(($tileMatrix->top - $extent[3]) / ($tileHeight * $res)); $maxRow = floor(($tileMatrix->top - $extent[1]) / ($tileHeight * $res)); $tileMatrixLimits[] = (object) array('id' => $k, 'minRow' => $minRow, 'maxRow' => $maxRow, 'minCol' => $minCol, 'maxCol' => $maxCol); } } $tileMatrixSetLink = (object) array('ref' => $tileMatrixSet->ref, 'tileMatrixLimits' => null); $tileMatrixSetLink->tileMatrixLimits = $tileMatrixLimits; $tileMatrixSetLinkList[] = $tileMatrixSetLink; } $l = (object) array('id' => $layer->id, 'name' => $layer->name, 'title' => $layer->title, 'abstract' => $layer->abstract, 'imageFormat' => $layer->imageFormat, 'lowerCorner' => $lowerCorner, 'upperCorner' => $upperCorner, 'minScale' => $layer->minScale, 'maxScale' => $layer->maxScale, 'tileMatrixSetLinkList' => null); $l->tileMatrixSetLinkList = $tileMatrixSetLinkList; return $l; }
} include $_SERVER['argv'][1]; $proj4 = new Proj4php(); $projSrc = new Proj4phpProj('EPSG:TM2', $proj4); $projDst = new Proj4phpProj('WGS84', $proj4); $twd67to97 = function ($point) { $A = 1.549E-5; $B = 6.521E-6; $X67 = $point[0]; $Y67 = $point[1]; $X97 = $X67 + 807.8 + $A * $X67 + $B * $Y67; $Y97 = $Y67 - 248.6 + $A * $Y67 + $B * $X67; return array($X97, $Y97); }; $fp = fopen('road.csv', 'r'); $output = fopen('php://output', 'w'); $columns = fgetcsv($fp); fputcsv($output, $columns); while ($rows = fgetcsv($fp)) { if ($rows[13] == 99 and $rows[14] == 99 or !$rows[13]) { $rows[13] = $rows[14] = ''; } else { // 有些資料 X, Y 寫反,所以加上 min, max 判斷 $point = $twd67to97(array(min($rows[13], $rows[14]), max($rows[13], $rows[14]))); $pointSrc = new proj4phpPoint($point[0], $point[1]); $pointDst = $proj4->transform($projSrc, $projDst, $pointSrc); $rows[13] = $pointDst->x; $rows[14] = $pointDst->y; fputcsv($output, $rows); } }
public function hasNext() { /** * We have to return our information in non-hierarchical manner! * This brings some complications with handling shp files ofourcse * This class will be used as a normal reader would be used namely * while(hasNext()){ $data = pop()} * * Since our records are hierarchical we will deliver a flattened object of the record since the ETML expects this * */ if (($record = $this->shape_file_wrapper->getNext()) != false) { // read meta data $rowobject = array(); $dbf_data = $record->getDbfData(); foreach ($dbf_data as $property => $value) { $property = strtolower($property); $rowobject[$property] = trim($value); } $shp_data = $record->getShpData(); if (isset($shp_data['parts']) || $shp_data['x']) { // read shape data if ($this->EPSG != "") { $proj4 = new \Proj4php(); $projSrc = new \Proj4phpProj('EPSG:' . $this->EPSG, $proj4); $projDest = new \Proj4phpProj('EPSG:4326', $proj4); } if (isset($shp_data['parts'])) { $parts = array(); foreach ($shp_data['parts'] as $part) { $points = array(); foreach ($part['points'] as $point) { $x = $point['x']; $y = $point['y']; if ($this->EPSG != "" || true) { $pointSrc = new \proj4phpPoint($x, $y); $pointDest = $proj4->transform($projSrc, $projDest, $pointSrc); $x = $pointDest->x; $y = $pointDest->y; } $points[] = $x . ',' . $y; } array_push($parts, implode(" ", $points)); } $rowobject["coords"] = implode(';', $parts); } if (isset($shp_data['x'])) { $x = $shp_data['x']; $y = $shp_data['y']; if ($EPSG != "") { $pointSrc = new \proj4phpPoint($x, $y); $pointDest = $proj4->transform($projSrc, $projDest, $pointSrc); $x = $pointDest->x; $y = $pointDest->y; } $rowobject["long"] = $x; $rowobject["lat"] = $y; } } $this->read_record = $rowobject; return true; } else { return false; } }