X

Geertjan's Blog

  • May 10, 2012

Geotk & NetBeans Platform

Geertjan Wielenga
Product Manager

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!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.