[OpenLayers-Users] how to save polygons or lines

Burgholzer,Robert rwburgholzer at deq.virginia.gov
Wed Oct 17 08:56:41 EDT 2007


I went through this myself, and designed a small routine to do this
(this has been posted in a shorter form previously).  This routine is
NOT done entirely in Ajax, and therefore, I think that it is not an
optimal solution (I will be doing an AJAX based component shortly).  If
you want to develop your own, however, this will at least provide a
basic understanding of the OL classes, methods and an approach on the
PostGIS side.  I use PHP as my interface between OL and PostGIS.

Since I based my work off the WFS demo, I accomplished this by adding a
method to OpenLayers\Layers\WFS.js file. I added this new method, called
"commitPassthru", so that I did not screw with anything in the existing
class.  I will be doing an entirely separate class, perhaps based on the
GML layer, since I use WKB as the common tongue for linking OL and
PostGIS. 

So, here is the .js method, commitpassthru, which takes the shape that
is drawn (all based on WFS-t.html in the examples directory).
Basically, when someone clicks the "Save Scratch" link on the page, it
calls the commitpasthru method of the layer, commitpassthru then
iterates through every shape that has been drawn, turns it into a WKT
string, and stores it in a multi-dimensional array for POST'ing to the
PHP script.  When it has gotten all shapes formatted, it sends the
request to the php script identified by the property "pgurl" :
    commitPassthru: function() {
        var url = this.pgurl;
        if (OpenLayers.ProxyHost && this.url.startsWith("http")) {
            url = OpenLayers.ProxyHost + escape(this.url);
        }
        var success = this.commitSuccess.bind(this);

        var failure = this.commitFailure.bind(this);
        // passing variabels via POST request
        //data = OpenLayers.Ajax.serializeXMLToString(data);
        // from prototype.js
        var pParams = "REQUEST=UPDATE";
        //pParams += "&XMLDATA=";
        //pParams += data;

        var wktObj = new OpenLayers.Format.WKT;

        for(var i=0; i<this.features.length; ++i) {
           var wktdata = wktObj.write(this.features[i]);
           pParams += "&WKTDATA[";
           pParams += i;
           pParams += "]=";
           pParams += wktdata;
	    }

        var responseObj = new OpenLayers.Ajax.Request(url,
		   {  method: 'post',
			  //postBody: pParams,
			  parameters: pParams,
			  onSuccess: function(transport) {
			     //alert('Successful Connection');
			  },
			  onComplete: function(transport) {
				 alert(transport.responseText);
			  }
		    }
		);},


The PHP script that handles the request is as follows (where listobject
is a custom interface to a pg data source, basically takes the place of
pg_exec calls).  This script basically iterates through the WKTDATA
array in the post, getting the shapes, and then inserts hem into the
database, and returns a strinq containing the queries that performed the
inserts (which the commitpassthru method shows via a javascript "alert"
box):
include('./clsParseXML.php');
include('./config.php');

if (isset($_POST)) {
   //print_r($_REQUEST);
   //print_r($_POST);
   //print_r($_GET);
}

$xmlparse = &new ParseXML;
#$xml = $xmlparse->parseTree($_POST['XMLDATA']);
$wkt = $_POST['WKTDATA'];
#print_r($wkt);
$i = 3;
foreach ($wkt as $thiswkt) {
   $listobject->querystring = "  insert into proj_features (gid, name,
the_geom) ";
   $listobject->querystring .= " select $i, 'test shape $i',
GeomFromText('$thiswkt', 4326) ";
   print("$listobject->querystring ");
   $listobject->performQuery();
   $i++;
}



The link that calls the save function is:
<a href="wfs-t.php" onclick="map.layers[2].commitPassthru();return
false">Save Scratch</a>

HTH,
r.b.

Robert W. Burgholzer
Surface Water Modeler
Office of Water Supply and Planning
Virginia Department of Environmental Quality
rwburgholzer at deq.virginia.gov
804-698-4405
Open Source Modeling Tools:
http://sourceforge.net/projects/npsource/
Web-Based Water Supply Planning Demo:
http://soulswimmer.dynalias.net/models/wsdemo/demo_hsi.php

-----Original Message-----
From: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org]
On Behalf Of R. Ortner
Sent: Wednesday, October 17, 2007 7:15 AM
To: users at openlayers.org
Subject: Re: [OpenLayers-Users] how to save polygons or lines


Thats basically what i was searching but is it possible to write those
data
into my postGIS database and load it from there? bec. i have not really
an
idea what that featureserver is and how i can use it according to my
postgis
db!?


Christopher Schmidt-4 wrote:
> 
> On Mon, Oct 15, 2007 at 12:51:27AM -0700, R. Ortner wrote:
>> 
>> i addet the EditingToolbar to my map and now i like to save the drawn
>> polygons and lines to a file so that i can racall them when i reload
the
>> map. is that possible?
> 
> It's possible to serialize features to a number of formats:
> 
> http://openlayers.org/dev/examples/vector-formats.html
> 
> Once you do, it's possible to push that data up to a server using
> OpenLayers.Ajax.
> 
> Using that mechanism, you can save your data to the server, like
> the FeatureServer demo does: 
> 
> http://featureserver.org/demo.html
> 
> Regards,
> -- 
> Christopher Schmidt
> MetaCarta
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> 

-- 
View this message in context:
http://www.nabble.com/how-to-save-polygons-or-lines-tf4625017.html#a1325
1374
Sent from the OpenLayers Users mailing list archive at Nabble.com.

_______________________________________________
Users mailing list
Users at openlayers.org
http://openlayers.org/mailman/listinfo/users



More information about the Users mailing list