Using a LayerViewPanel to display ESRI Shapefiles

94 views
Skip to first unread message

Cride5

unread,
Nov 3, 2008, 11:28:27 PM11/3/08
to openjump-users
Hi, I've been trying to use OpenJump's API to display ESRI shapefiles
in my own Java application.

Using the online API here:
http://jump-pilot.sourceforge.net/javadoc/openjump_javadoc/
... and the WIKI here:
http://openjump.org/wiki/show/HomePage
... I wasn't able to find much info on how to do this. My attempts so
far
go something like this:

-----------------[ Start Code ]--------------

// Initialise panel for displaying layers
LayerViewPanel view = new LayerViewPanel(new
LayerManager(), new LayerViewPanelContext(){
public void setStatusMessage(String m){
System.out.println("STATUS: " + m); }
public void warnUser(String m){
System.out.println("WARNING: " + m); }
public void handleThrowable(Throwable t){
t.printStackTrace(); }
});
view.setViewportInitialized(false);
view.getRenderingManager().setPaintingEnabled(true);

// Read shape file
ShapefileReader reader = new ShapefileReader();
FeatureCollection features = null;
try{ features = reader.read(new DriverProperties("/home/
cride5/data/fields.shp"));
}catch(Exception e){ e.printStackTrace(); }
// Ensure data has been read - returns: New layer has
41 features
System.out.println("New layer has " + features.size() +
" features");

// Add layer with features
Layer layer = view.getLayerManager().addLayer("cat1",
"fields", features);
layer.getBasicStyle().setFillColor(Color.GREEN);

// Initialise window and add layer view panel
JFrame frame = new JFrame("Jump Test");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(640, 480));
frame.add(view, BorderLayout.CENTER);
frame.setVisible(true);

// Try various functions to display data
view.fireSelectionChanged();
view.getSelectionManager().updatePanel();
view.getRenderingManager().render(layer);
view.getRenderingManager().renderAll();
view.getRenderingManager().repaintPanel();
view.repaint();
view.superRepaint();

----------------[ End code ]------------------

Is there something I've done wrong, or something extra required to
make this work? Any info would be a great help!

Cheers,

Conrad

Larry Becker

unread,
Nov 4, 2008, 9:45:30 AM11/4/08
to openjum...@googlegroups.com
Hi Conrad,

  Wow, it would be great if you could make a GIS viewer based on JUMP with that little code!  I suspect the answer is much more complex.  I'm not aware of any prior efforts that have succeeded without carrying the rest of the baggage along too.  I know that the JUMP developers guide touts the LayerViewPanel as a component that can be used in other applications, so perhaps it can be done.  We're rooting for you.  If you have any specific questions, just ask.

regards,
Larry Becker
--
http://amusingprogrammer.blogspot.com/

Sunburned Surveyor

unread,
Nov 4, 2008, 11:16:20 AM11/4/08
to openjum...@googlegroups.com
Conrad,

I'm also interested in what you are trying to do. It would be nifty if
we could package the LayerViewPanel as a working viewer of Features
with as little baggage as possible.

As Larry mentioned, this may be harder to do than it sounds. In the
code you pasted above, I didn't see where you added the LayerViewPanel
to your JFrame. Did I miss that part of your code?

It might be helpful if you past the rest of your code. What happens
when you run your main method? Is an exception thrown? Does the JFrame
display? There are a lot of things that could go wrong with the code
above, or code that might be missing. If we have the rest of the
application we might be able to figure this out.

The Sunburned Surveyor

Cride5

unread,
Nov 4, 2008, 1:23:25 PM11/4/08
to openjump-users
Hi, thanks for your responses. I guess I was just hacking away to try
to get the thing to work. The main problem, though, is that there
aren't a lot of comments in the javadoc to go by, so it's mainly just
guesswork.

Because the application I'm developing is for use in human
experiments, I need very strict control over what the user (in this
case experiment subject) gets to see. This is reason I'm trying to do
it without including the rest of the baggage from the rest of program.

The LayerViewPanel is added to the frame in the line:
frame.add(view, BorderLayout.CENTER);

The result of running the code is:
(1) the shapefile seems to be loaded OK because the line:
System.out.println("New layer has " + features.size() + " features");
... yields:
New layer has 41 features
(2) a window with the title "Jump Test" appears, 640 x 480 in
dimensions and with a white background colour (which a believe is
Jump's default canvas colour).

Interestingly, when I hover over the window with my mouse I get the
following exception thrown :
-------------[ start exception ]----------------
java.awt.geom.NoninvertibleTransformException: Determinant is 0
at
java.awt.geom.AffineTransform.inverseTransform(AffineTransform.java:
3466)
at
com.vividsolutions.jump.workbench.ui.Viewport.toModelPoint(Viewport.java:
174)
at com.vividsolutions.jump.workbench.ui.LayerViewPanel
$2.mouseLocationChanged(LayerViewPanel.java:141)
at com.vividsolutions.jump.workbench.ui.LayerViewPanel
$2.mouseMoved(LayerViewPanel.java:136)
at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:
312)
at java.awt.Component.processMouseMotionEvent(Component.java:6083)
at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:
3283)
at java.awt.Component.processEvent(Component.java:5807)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3999)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:
273)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:
183)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:
173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
----------[ end exception ]-----------
.. which must be something to do with event handlers trying to handle
mouse motions and no code to deal with it. Removing the mouse motion
listeners is a quick fix:
------------[ start code ]--------------
MouseMotionListener[] listeners = view.getMouseMotionListeners();
for(int i = 0; i < listeners.length; i++){
view.removeMouseMotionListener(listeners[i]);
}
----------[ end code ]----------------

Because the problem is visual, its quite difficult to debug. A couple
lines of enquiry are:
(1) ensure the zoom level is correct
(2) ensure the layer view is panned to the data's location
(3) ensure the renderer is applying some kind of style to make it
visible

I'll come back if I make any progress. If anyone has any breakthroughs
do let me know.

Regards,

Conrad

On Nov 4, 4:16 pm, "Sunburned Surveyor" <sunburned.surve...@gmail.com>
wrote:
> Conrad,
>
> I'm also interested in what you are trying to do. It would be nifty if
> we could package the LayerViewPanel as a working viewer of Features
> with as little baggage as possible.
>
> As Larry mentioned, this may be harder to do than it sounds. In the
> code you pasted above, I didn't see where you added the LayerViewPanel
> to your JFrame. Did I miss that part of your code?
>
> It might be helpful if you past the rest of your code. What happens
> when you run your main method? Is an exception thrown? Does the JFrame
> display? There are a lot of things that could go wrong with the code
> above, or code that might be missing. If we have the rest of the
> application we might be able to figure this out.
>
> The Sunburned Surveyor
>
> On Tue, Nov 4, 2008 at 6:45 AM, Larry Becker <becker.la...@gmail.com> wrote:
> > Hi Conrad,
>
> >   Wow, it would be great if you could make a GIS viewer based on JUMP with
> > that little code!  I suspect the answer is much more complex.  I'm not aware
> > of any prior efforts that have succeeded without carrying the rest of the
> > baggage along too.  I know that the JUMP developers guide touts the
> > LayerViewPanel as a component that can be used in other applications, so
> > perhaps it can be done.  We're rooting for you.  If you have any specific
> > questions, just ask.
>
> > regards,
> > Larry Becker
>

Cride5

unread,
Nov 4, 2008, 1:32:09 PM11/4/08
to openjump-users
Ooops, for got to say. If you want a self-contained version of the
code just wrap it up in a public static void main, not forgetting the
necessary imports. For example:

-----------[ start Jump.java ]------------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import com.vividsolutions.jump.feature.FeatureCollection;
import com.vividsolutions.jump.io.DriverProperties;
import com.vividsolutions.jump.io.ShapefileReader;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.model.LayerManager;
import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
import com.vividsolutions.jump.workbench.ui.LayerViewPanelContext;

/**
* Test loading and visualising of vector data
* Should allow the application and alteration of visualised
attributes
*/
public class Jump {

public static void main(String[] args){

// Initialise panel for displaying layers
LayerViewPanel view = new LayerViewPanel(new LayerManager(), new
LayerViewPanelContext(){
public void setStatusMessage(String m){
System.out.println("STATUS: " + m); }
public void warnUser(String m){
System.out.println("WARNING: " + m); }
public void handleThrowable(Throwable t){
t.printStackTrace(); }
});
view.setViewportInitialized(false);
view.getRenderingManager().setPaintingEnabled(true);
// Remove mouse motion listeners
MouseMotionListener[] listeners = view.getMouseMotionListeners();
for(int i = 0; i < listeners.length; i++){
view.removeMouseMotionListener(listeners[i]);
}

// Read shape file
ShapefileReader reader = new ShapefileReader();
FeatureCollection features = null;
try{ features = reader.read(new DriverProperties("/home/cride5/data/
fields.shp"));
}catch(Exception e){ e.printStackTrace(); }
// Ensure data has been read - returns: New layer has 41 features
System.out.println("New layer has " + features.size() + "
features");

// Add layer with features
Layer layer =
view.getLayerManager().addLayer(StandardCategoryNames.WORKING,
"fields", features);
layer.getBasicStyle().setFillColor(Color.GREEN);

// Initialise window and add layer view panel
JFrame frame = new JFrame("Jump Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(640, 480));
frame.add(view, BorderLayout.CENTER);
frame.setVisible(true);

// Try various functions to display data
view.fireSelectionChanged();
view.getSelectionManager().updatePanel();
view.getRenderingManager().render(layer);
view.getRenderingManager().renderAll();
view.getRenderingManager().repaintPanel();
view.repaint();
view.superRepaint();

}
}

------------[ end Jump.java ]------------

Sunburned Surveyor

unread,
Nov 5, 2008, 10:33:10 AM11/5/08
to openjum...@googlegroups.com
Did removing the mouse listeners from the LayerViewPanel fixe the
problem Cride5? Based on the stack trace you posted, I wonder if the
LayerViewPanel is expecting a default CursorTool to handle mouse
events. That shouldn't be too hard to hack around.

Let me know.

The Sunburned Surveyor

Cride5

unread,
Nov 6, 2008, 3:59:58 PM11/6/08
to openjump-users
Removing the mouse motion listeners did prevent any exceptions from
being thrown, but still no map being displayed. What ever the problem
is, it doesn't seem to be causing Jump to complain. I'm going to have
another go at it tonight...

On Nov 5, 3:33 pm, "Sunburned Surveyor" <sunburned.surve...@gmail.com>
wrote:
> Did removing the mouse listeners from the LayerViewPanel fixe the
> problem Cride5? Based on the stack trace you posted, I wonder if the
> LayerViewPanel is expecting a default CursorTool to handle mouse
> events. That shouldn't be too hard to hack around.
>
> Let me know.
>
> The Sunburned Surveyor
>
> ...
>
> read more »

Larry Becker

unread,
Nov 6, 2008, 4:14:39 PM11/6/08
to openjum...@googlegroups.com
See com.vividsolutions.jump.workbench.ui.LayerPrinter for an example of creating a a LayerViewPanel and rendering to it.

Larry
--
http://amusingprogrammer.blogspot.com/

Cride5

unread,
Nov 6, 2008, 4:48:23 PM11/6/08
to openjump-users
Success! My suspicions turned out to be correct. The features were
actually there, but somewhere outside of the viewport. The solution is
to zoom to the extent of the features using:
view.getViewport().zoomToFullExtent();

For the benefit of future Jump users, I've posted a basic Shapefile
viewer below - maybe something like this could be added to the
documentation?

-------------------[ Jump.java ]--------------

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;

import com.vividsolutions.jump.feature.FeatureCollection;
import com.vividsolutions.jump.io.DriverProperties;
import com.vividsolutions.jump.io.ShapefileReader;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.model.LayerManager;
import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
import com.vividsolutions.jump.workbench.ui.LayerViewPanelContext;

/**
* Test loading and visualising of vector data
* AUTHOR: Conrad Rider (www.crider.co.uk)
*/
public class Jump {

// Shapefiles to load and display
public static final String[] SHAPEFILES = new String[]{
"/home/cride5/data/fields.shp",
"/home/cride5/data/roads.shp",
"/home/cride5/data/river.shp",
"/home/cride5/data/buildings.shp",
};

public static void main(String[] args){

// Initialise panel for displaying layers
LayerViewPanel view = new LayerViewPanel(new LayerManager(), new
LayerViewPanelContext(){
public void setStatusMessage(String m){
System.out.println("STATUS: " + m); }
public void warnUser(String m){
System.out.println("WARNING: " + m); }
public void handleThrowable(Throwable t){
t.printStackTrace(); }
});
// Remove (unregister) mouse motion listeners
// prevents unhandled events causing exceptions
MouseMotionListener[] listeners = view.getMouseMotionListeners();
for(int i = 0; i < listeners.length; i++){
view.removeMouseMotionListener(listeners[i]);
}

// Read shape files
ShapefileReader reader = new ShapefileReader();
List<FeatureCollection> featureList = new
LinkedList<FeatureCollection>();
for(int i = 0; i < SHAPEFILES.length; i++){
try{
FeatureCollection features = reader.read(new
DriverProperties(SHAPEFILES[i]));
// Report size of read data
System.out.println("Read " + features.size() + " features from " +
SHAPEFILES[i]);
// Add data to list
featureList.add(features);
}catch(Exception e){
// Report error on read failure
System.out.println("Error reading features from " +
SHAPEFILES[i]);
e.printStackTrace();
}
}

// Create and add layer for each feature set (shapefile)
for(FeatureCollection features : featureList){
Layer layer = view.getLayerManager().addLayer(
"Category Name", "Layer Name", features);
}

// Initialise window and add layer view panel
JFrame frame = new JFrame("Jump Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(640, 480));
frame.add(view, BorderLayout.CENTER);
frame.setVisible(true);

// Pan and zoom to extent of features
try{ view.getViewport().zoomToFullExtent(); }
catch(Exception e){ e.printStackTrace(); }
}
}

--------------------------------------------------

On Nov 5, 3:33 pm, "Sunburned Surveyor" <sunburned.surve...@gmail.com>
wrote:
> Did removing the mouse listeners from the LayerViewPanel fixe the
> problem Cride5? Based on the stack trace you posted, I wonder if the
> LayerViewPanel is expecting a default CursorTool to handle mouse
> events. That shouldn't be too hard to hack around.
>
> Let me know.
>
> The Sunburned Surveyor
>
> ...
>
> read more »

Sunburned Surveyor

unread,
Nov 6, 2008, 6:07:16 PM11/6/08
to openjum...@googlegroups.com
Cride5 wrote: "For the benefit of future Jump users, I've posted a

basic Shapefile
viewer below - maybe something like this could be added to the
documentation?"

Absolutely. I'll try and get it added to the wiki this week.

The Sunburned Surveyor

MIDHUNLAL P V

unread,
Sep 15, 2015, 2:13:18 PM9/15/15
to openjump-users
layerviewpanel is displaying shapefiles.
Can layerviewpanel zoomed with zoomtool?If anybody knows please reply
Thanks for reading

edgar....@web.de

unread,
Sep 16, 2015, 9:55:51 AM9/16/15
to openjum...@googlegroups.com
On 13.09.2015 19:34, MIDHUNLAL P V wrote:
> layerviewpanel is displaying shapefiles.
> Can layerviewpanel zoomed with zoomtool?If anybody knows please reply

of course. use shortcuts Ctrl and Minus(-),Plus(+),Zero(0) or the ZoomTool available in the Main Toolbar or the Editing Toolbox. The icon is a looking glass.

..ede
Reply all
Reply to author
Forward
0 new messages