Geotk & NetBeans Platform

An alternative to OpenMap is Geotk. (Read here about the relationship between Geotk and GeoTools.) I recently wrote about how to get started setting up OpenMap in a NetBeans Platform application.

Now it's Geotk's turn. 

Here's some code to get a first impression of how it works:

public class Map extends JFrame {

    public Map() {
        initComponents();
        setLayout(new BorderLayout());
        MapContext mapContext = MapBuilder.createContext(DefaultGeographicCRS.WGS84);
        addShpData(mapContext);
        JMap2D map = new JMap2D();
        map.getContainer().setContext(mapContext);
        map.setBackground(new Color(0, 150, 150));
        JNavigationBar navBar = new JNavigationBar(map);
        add(navBar, BorderLayout.NORTH);
        add(map, BorderLayout.CENTER);
    }

    public static void addShpData(MapContext context) {
        
        try {
            
            DataStore store = DataStoreFinder.
                    getDataStore("url", Map.class.getResource("/some/path/Countries.shp"));
            
            Name name = store.getNames().iterator().next();
            
            FeatureCollection fs = 
                    store.createSession(true).getFeatureCollection(QueryBuilder.all(name));
            
            MapLayer layer = MapBuilder.createFeatureLayer(fs,
                    RandomStyleFactory.createRandomVectorStyle(fs));
            
            layer.setVisible(true);
            
            context.layers().add(layer);
        
        } catch (DataStoreException e) {
        }

    }

    ...
    ...
    ...

When the application above is run, the following is shown, depending on the selected Shape file:

Next, of course, I want to do the above within a NetBeans Platform application.

Here's the code I used, in a TopComponent, i.e., a copy/paste of the code above:

public final class MapTopComponent extends TopComponent {

    public MapTopComponent() {
        initComponents();
        setName(Bundle.CTL_MapTopComponent());
        setToolTipText(Bundle.HINT_MapTopComponent());
        setLayout(new BorderLayout());
        MapContext mapContext = MapBuilder.createContext(DefaultGeographicCRS.SPHERE);
        addShpData(mapContext);
        JMap2D map = new JMap2D();
        map.getContainer().setContext(mapContext);
        map.setBackground(new Color(0, 150, 150));
        JNavigationBar navBar = new JNavigationBar(map);
        add(navBar, BorderLayout.NORTH);
        add(map, BorderLayout.CENTER);
    }

    public void addShpData(MapContext context) {

        try {

            DataStore store = DataStoreFinder.getDataStore("url", 
                    MapTopComponent.class.getResource("Countries.shp"));

            Name name = store.getNames().iterator().next();

            Session session = store.createSession(true);

            FeatureCollection fs = session.getFeatureCollection(QueryBuilder.all(name));

            MapLayer layer = MapBuilder.createFeatureLayer(fs,
                    RandomStyleFactory.createDefaultVectorStyle(fs));

            layer.setVisible(true);

            context.layers().add(layer);

        } catch (DataStoreException e) {
        }

    }
    ...
    ...
    ...

However, when I run the above, I see the following:

The message in the error dialog above is:

"The ShapeFileDatastore does not support sortby query."

I wonder why this problem occurs, since the code works fine in a standard Java Swing application, but not in the NetBeans Platform. I have a feeling that the DataStore class is resolved to a different implementation in standard Java Swing to when used within the NetBeans Platform.

I asked Johann Sorel from the Geotk project for advice, and he told me: "Shapefile are flat files with few indexes, a sort by would cause the datastore to read the full file and potentialy take much memory." I tried various solutions based on what Johann sent, but so far without any luck. Hope to get this working!

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
21
22
23
24
25
26
27
28
29
30
   
       
Today