Supposing I have created a Vector Layer in OpenLayers and then transformed it to WKT or GeoJson, how can I export it from OpenLayers, and possible import it to PostGIS or GeoServer?
-
I think you have to be more specific. You want to add a feature to a PostGis table from a web interface? Or do you want to add a feature to a WFS-T using GeoServer? Both things can be done relatively easy, but are very different in nature– plablo09Oct 13, 2014 at 15:05
-
@plablo09 I have a Vector Layer in my Openlayers code. A set of Geometry points. And I need to put that in my database.– drizoOct 13, 2014 at 15:17
-
Yeah well, that can be achieved in a number of ways. If you wanna go down the services path, then you can try using a WFS in geoserver and configure a save strategy in OpenLayers as in this question: gis.stackexchange.com/questions/76259/…– plablo09Oct 13, 2014 at 15:25
-
Ok so this way I can save my layer to GeoServer only by running the website?– drizoOct 13, 2014 at 15:44
-
Well, not quite, you have to configure some action in your website to trigger the save event. I'll try to write a concise answer to your question in a couple of hours– plablo09Oct 13, 2014 at 17:50
1 Answer
Lets assume you want to save a feature from a OpenLayers Vector Layer into PostGis and that you want to achieve this using GeoServer. The general steps would be as follows:
- You need to configure a WFS in geoserver using PostGis as a data store (GeoServer publish posgis table)
You need to configure your vectyor layer to point to the wfs you just created and a Save Strategy:
saveStrategy = new OpenLayers.Strategy.Save(); saveStrategy.events.register("success", '', alert('succes')); saveStrategy.events.register("failure", '', alert('failure')); wfs = new OpenLayers.Layer.Vector("Some Title", { strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy], projection: 'EPSG:4326', protocol: new OpenLayers.Protocol.WFS({ version: '1.1.0', url: 'http://xxx.xxx.x.xx:8080/geoserver/wfs', featurePrefix: 'cite', featureNS: 'http://www.opengeospatial.net/cite', featureType: 'ODC_Private_Graphics_Data', geometryName: 'the_geom', extractAttributes: true, srsName: 'EPSG:4326', isBaseLayer: false, visibility: true, schema: 'http://xxx.xxx.x.xx:8080/geoserver/wfs/DescribeFeatureType?version=1.1.0&;typename=cite:ODC_Private_Graphics_Data',
});
You need to configure some action in your html/js code to trigger the save event of the save strategy, for the sake of simplicity, lets assume that you configure a simple link to trigger the event on the OnClick action:
<a href="#" onClick="saveFeatures">Save Features</a> function saveFeatures(){ saveStrategy.save() }
There are a few things that you need to take care. First, you need to pay special attention to the featurePrefix, featureNS and featureType parameters since these can be tricky (there is plenty documentation on this). Second, this code is for OpenLayers 2.x so, if you're using 3.x you should modify accordingly.You can see a fu8ll working example here
Finally, you can save a feature directly to PostGis without a wfs with some server side code that executes the insert statement directly and a simple ajax call, perhaps that would be a simpler solution, but in general, services should be the way to go
-
Good answer. I have always found WFS-T spectacularly painful to get right and have always written my own server-side code, either to store geojson directly (which is great because you can bundle attributes in with the geometries, and load these directly into OpenLayers) or WKT to geometry in Postgis. We are talking very small amounts of code. In know that in theory services are the way to go, but in practice I have found the pain far outweighs the gain. Oct 14, 2014 at 6:43
-
I can't understand how I should configure GeoServer, Step 1. Maybe this link is about it ? docs.geoserver.org/2.5.x/en/user/data/cascaded/wfs.html Do I need to connect WFS layer to a layer that already exists in GeoServer or it gets created by javascript– drizoOct 15, 2014 at 8:04
-
@plablo09 I mean the layer name has to be a layer that exists in Geoserver and PostGIS or does the save function create the wfs layer?– drizoOct 15, 2014 at 9:45
-
@plablo09 for some reason I dont know the messages which I wrote exactly like you, they pop up immediately when I load the page..– drizoOct 15, 2014 at 10:23
-
@drizo Yes, the layer should exist in Geoserver and it should point to a table in postgis. You don't need to cascade it, it's a simple and straightforward wfs with postgis as backend. The process to serve a wfs layer from geoserver is exactly the same as a wms, you don`t need to do anything special, just configure the data store and add the layer as per the docs. (of course, you should note the security issues but that's for later)– plablo09Oct 15, 2014 at 14:51