user1317252
user1317252

Reputation: 21

Display POSTGIS data in a JMapFrame with GeoTools

EDIT: For information : create style with StyleLab example and it will display what you want.

I'm trying to display POSTGIS data with GeoTools I did the examples in : http://docs.geotools.org/stable/userguide/examples/ : With QueryLab i can display a tab of the POSTGIS data With QuickStart I can display a map of a shapefile (.shp)

But I didn't succeed in mixing those source codes to display a map with my postgis datas

Regarding the error message, it can come from the absence of style definition. Nevertheless it works perfectly for shapefiles so I don't understand. Furthermore, I don't find how to create appropriate style to sort out this problem.

How can I display the POSTGIS Geometries into a map ? Does anyone know how to solve this issue or have any ideas ?

here my source code and message error :

package org.geotools.tuto;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.map.*;
import org.geotools.swing.JMapPane;

public class test {
    public test() throws IOException{
        Map params = new HashMap();
        params.put("dbtype", "postgis");  //must be postgis
        //the name or ip address of the machine running PostGIS
        params.put("host", "localhost");
        //the port that PostGIS is running on (generally 5432)
        params.put("port", new Integer(5432));
        //the name of the database to connect to.
        params.put("database", "***");
        params.put("user", "***");         //the user to connect with
        params.put("passwd", "***");               //the password of the user.

        FeatureSource fsBC = null;
        DataStore pgDatastore;
        try {
            pgDatastore = DataStoreFinder.getDataStore(params);

            fsBC = pgDatastore.getFeatureSource("pumas_sections");
            System.out.println("bc count: " + fsBC.getCount(Query.ALL));
            } catch (IOException e) {
            e.printStackTrace();
        }

        MapContext map = new DefaultMapContext();
        map.setTitle("Quickstart");
        map.addLayer(fsBC, null);

        //...
    }

    public static void main(String[] args) throws Exception {
            test t = new test();
    }
}

error:

Exception in thread "main" java.lang.UnsupportedOperationException: No
style method for com.vividsolutions.jts.geom.Geometry
        at org.geotools.styling.SLD.createSimpleStyle(SLD.java:1967)
        at org.geotools.styling.SLD.createSimpleStyle(SLD.java:1923)
        at org.geotools.map.DefaultMapContext.checkStyle(DefaultMapContext.java:389)
        at org.geotools.map.DefaultMapContext.addLayer(DefaultMapContext.java:222)
        at org.geotools.tuto.test.<init>(test.java:45)
        at org.geotools.tuto.test.main(test.java:52)

Upvotes: 2

Views: 2498

Answers (1)

CafeGIS
CafeGIS

Reputation: 11

I did try your code, and it worked. I used geotools 10, after getting fsBC, I used MapContent.

Style style = SLD.createSimpleStyle(fsBC.getSchema());
Layer layer = new FeatureLayer(fsBC, style);
MapContent map =new MapContent();
map.addLayer(layer);

Hope this useful.

Upvotes: 1

Related Questions