[Mapbender-commits] r8886 - in trunk/mapbender: http/php lib
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Wed May 21 05:29:21 PDT 2014
Author: hwbllmnn
Date: 2014-05-21 05:29:21 -0700 (Wed, 21 May 2014)
New Revision: 8886
Added:
trunk/mapbender/http/php/transformgeojson.php
Modified:
trunk/mapbender/lib/mb.ui.displayKmlFeatures.js
Log:
use server side transformation for geometries for better accuracy
Added: trunk/mapbender/http/php/transformgeojson.php
===================================================================
--- trunk/mapbender/http/php/transformgeojson.php (rev 0)
+++ trunk/mapbender/http/php/transformgeojson.php 2014-05-21 12:29:21 UTC (rev 8886)
@@ -0,0 +1,84 @@
+<?php
+# http://www.mapbender.org/index.php/Administration
+# Copyright (C) 2002 CCGIS
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+require_once(dirname(__FILE__)."/../php/mb_validateSession.php");
+
+header("Content-Type: application/json");
+
+$json = file_get_contents("php://input");
+try {
+
+ $json = json_decode($json);
+ $epsg = explode(':', $_GET["targetEPSG"]);
+ $epsg = $epsg[1];
+
+ $gui_id = Mapbender::session()->get("mb_user_gui");
+ $con = db_connect($DBSERVER,$OWNER,$PW);
+ db_select_db(DB,$con);
+
+
+ foreach($json as $feat) {
+ if(preg_match('/point/i', $feat->geometry->type)) {
+ $sql = "SELECT st_asgeojson(st_transform(st_setsrid(st_geomfromtext($1), 4326), $2)) as geom";
+ $v = array('POINT(' . $feat->geometry->coordinates[0] . ' ' . $feat->geometry->coordinates[1] . ')', $epsg);
+ $t = array('s', 'i');
+ $res = db_prep_query($sql,$v,$t);
+ db_fetch_row($res);
+ $geom = json_decode(db_result($res, 0, 'geom'));
+ $feat->geometry = $geom;
+ }
+ if(preg_match('/linestring/i', $feat->geometry->type)) {
+ $sql = "SELECT st_asgeojson(st_transform(st_setsrid(st_geomfromtext($1), 4326), $2)) as geom";
+ $geom = 'LINESTRING(';
+ $coords = array();
+ foreach($feat->geometry->coordinates as $coord) {
+ $coords[] = $coord[0] . ' ' . $coord[1];
+ }
+ $geom = $geom . implode(',', $coords) . ')';
+ $v = array($geom, $epsg);
+ $t = array('s', 'i');
+ $res = db_prep_query($sql,$v,$t);
+ db_fetch_row($res);
+ $geom = json_decode(db_result($res, 0, 'geom'));
+ $feat->geometry = $geom;
+ }
+ if(preg_match('/polygon/i', $feat->geometry->type)) {
+ $sql = "SELECT st_asgeojson(st_transform(st_setsrid(st_geomfromtext($1), 4326), $2)) as geom";
+ $geom = 'POLYGON((';
+ $coords = array();
+ foreach($feat->geometry->coordinates[0] as $coord) {
+ $coords[] = $coord[0] . ' ' . $coord[1];
+ }
+ $geom = $geom . implode(',', $coords) . '))';
+ $v = array($geom, $epsg);
+ $t = array('s', 'i');
+ $res = db_prep_query($sql,$v,$t);
+ db_fetch_row($res);
+ $geom = json_decode(db_result($res, 0, 'geom'));
+ $feat->geometry = $geom;
+ }
+ }
+ $json = json_encode($json);
+ echo($json);
+
+} catch (Exception $e) {
+ echo($e);
+ die;
+}
+
+?>
Modified: trunk/mapbender/lib/mb.ui.displayKmlFeatures.js
===================================================================
--- trunk/mapbender/lib/mb.ui.displayKmlFeatures.js 2014-05-20 14:51:36 UTC (rev 8885)
+++ trunk/mapbender/lib/mb.ui.displayKmlFeatures.js 2014-05-21 12:29:21 UTC (rev 8886)
@@ -43,6 +43,7 @@
wgs84: new Proj4js.Proj('EPSG:4326'),
targetProj: new Proj4js.Proj('EPSG:25832'),
_kmls: {},
+ cache: {},
kmlOrder: [],
_popup : null,
_create: function(){
@@ -160,6 +161,7 @@
},
addGeometry: function(pts, url, attributesDialog) {
+ this.cache = {};
var $map = $(this.element).mapbender();
var self = this;
var tp = pts.closedPolygon ? geomType.polygon : (pts.closedLine ? geomType.line : geomType.point);
@@ -335,7 +337,6 @@
y: feature.geometry.coordinates[1]};
if(isNaN(pt.x) || isNaN(pt.y)) return;
if(!feature.preview) {
- pt = Proj4js.transform(this.wgs84, this.targetProj, pt);
pt = $('#mapframe1').mapbender().convertRealToPixel(pt);
}
if(isNaN(pt.x) || isNaN(pt.y)) return;
@@ -371,7 +372,6 @@
$.each(feature.geometry.coordinates, function(_, v) {
var pt = {x: v[0], y: v[1]};
if(!feature.preview) {
- pt = Proj4js.transform(self.wgs84, self.targetProj, pt);
pt = map.convertRealToPixel(pt);
}
if(isNaN(pt.x) || isNaN(pt.y)) return;
@@ -392,7 +392,6 @@
$.each(feature.geometry.coordinates[0], function(_, v) {
var pt = {x: v[0], y: v[1]};
if(!feature.preview) {
- pt = Proj4js.transform(self.wgs84, self.targetProj, pt);
pt = map.convertRealToPixel(pt);
}
if(isNaN(pt.x) || isNaN(pt.y)) return;
@@ -471,9 +470,32 @@
var canvas = Raphael(target, map.getWidth(), map.getHeight());
$.each(this.kmlOrder, function(_, url) {
var item = self._kmls[url];
- if(item && item.display) {
- $.each(item.data.features, $.proxy(self, self.renderFeature(canvas)));
+ var feats = item.data.features;
+
+ if(!map.getSrs().match(/:4326/)) {
+ if(!self.cache[url]) {
+ self.cache[url] = {};
+ }
+ feats = self.cache[url][map.getSrs()];
+ if(!feats) {
+ $.ajax({
+ url: '../php/transformgeojson.php?targetEPSG=' + map.getSrs(),
+ type: 'POST',
+ data: JSON.stringify(item.data.features),
+ success: function(data) {
+ if(!$.isArray(data)) {
+ data = JSON.parse(data);
+ }
+ self.cache[url][map.getSrs()] = data;
+ self.render();
+ }
+ });
+ }
}
+
+ if(item.display && feats) {
+ $.each(feats, $.proxy(self, self.renderFeature(canvas)));
+ }
});
}
More information about the Mapbender_commits
mailing list