Search code examples
javapostgisshapefilegeoserver

Dynamically update input to Geoserver


I am hoping someone can help me on this one as i have been trying to find the solution for a long time without success.

I have a polygon shape-file which i am displaying on browser using geoserver . I have color-coded the polygons (by creating an SLD file) based on the values of one of the attributes(say "count") in the shape-file.

My problem is that i want to update the "count" attribute (which is an integer) regularly(on a daily basis) and hence i want to reflect this change in the geoserver. I am getting the values for "count" attribute through a java service which i have up and running . But so far , i have not been able to find out a way through which i can update a single attribute(for all features) in a shape-file through java . The geoTools library helps me to read a shape-file but i could not find a way to modify an attribute using this library .

How do i go about this?Please help me with the following questions:

  1. Is geoserver a correct choice for my use case?
  2. Is there some way to update an attribute of a shape-file using java.?
  3. Should i continue using a shape-file or move to POSTGIS?
  4. Can we update attributes of features in POSTGIS using java.?

Solution

    1. If you only need to show one layer, and that layer is not very big (what is the size of the shapefile?) i will convert the file to geojson and show it directly in the browser.

    Geoserver is commonly used with shapefiles when the underlying data does not change. If not is common to use postgis

    1. Sure. Geotools can do it. But geoserver can also do it using WFS-T. Just enable it and use POST request or the geotools wrapper to update the data.

    I think that the best option is create a new user for you backed process and add a security rule that allows wfs.Transaction only for that user. You can use basic http authentication in your backed process to launch a POST http request that update the shapefile.

    1. It depends. If is one shapefile and it doesn't change a log (once a day) i will probably use in this order:

    geojson directly show in the browser (without geoserver) -> shapefile + geoserver -> postgis + geoserver

    1. Of course. A postgis layer is only a postgresql table with a column of type geometry. You can update your table as you can update any other table in postgresql.

    I can't provide a full example right now to update a shapefile with geotools but check2 this links.

    • Open the shapefile as show in the access section in the second link
    • Open a transaction as show in the first link
    • Use the setAttribute methods of the feature instances
    • Close the transaction