Hello,

I'm quite new to geotools and I'm trying to get to grips with it.
Trying to load some shapefiles (see attachment) into a MapContent which in turn 
is associated with a SwtMapPane I always get an exception that suggests that 
there's something wrong with the current CRS of the map pane:

java.lang.IllegalArgumentException: Argument "sourceCRS" should not be null.
        at 
org.geotools.referencing.operation.AbstractCoordinateOperationFactory.ensureNonNull(AbstractCoordinateOperationFactory.java:734)
        at 
org.geotools.referencing.operation.BufferedCoordinateOperationFactory.createOperation(BufferedCoordinateOperationFactory.java:249)
        at org.geotools.referencing.CRS.findMathTransform(CRS.java:1205)
        at org.geotools.referencing.CRS.findMathTransform(CRS.java:1173)
        at org.geotools.swt.SwtMapPane.setCrs(SwtMapPane.java:436)
        at org.geotools.swt.SwtMapPane.layerAdded(SwtMapPane.java:812)
        at org.geotools.map.MapContent.fireLayerAdded(MapContent.java:468)
        at 
org.geotools.map.MapContent$LayerList.addIfAbsent(MapContent.java:1063)
        at org.geotools.map.MapContent.addLayer(MapContent.java:369)
        at org.geotools.JSurveyMvn.App.fileOpen(App.java:87)
        at org.geotools.JSurveyMvn.App$1.widgetSelected(App.java:113)
        at 
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
        at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3540)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3161)
        at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
        at org.eclipse.jface.window.Window.open(Window.java:801)
        at org.geotools.JSurveyMvn.App.<init>(App.java:47)
        at org.geotools.JSurveyMvn.App.main(App.java:58)

When trying to explicitly set a CRS directly after associating the (empty) 
MapContext to the map pane (see line 149), I'll get a NullPointerException 
like that:

java.lang.NullPointerException
        at org.geotools.swt.SwtMapPane.setCrs(SwtMapPane.java:433)
        at org.geotools.JSurveyMvn.App.createContents(App.java:152)
        at org.eclipse.jface.window.Window.create(Window.java:431)
        at org.eclipse.jface.window.Window.open(Window.java:790)
        at org.geotools.JSurveyMvn.App.<init>(App.java:47)
        at org.geotools.JSurveyMvn.App.main(App.java:58)


I'm quite desparate of being unable to solve that problem. Can you please give 
me a hand to make it work?

Attached is the source code of an example application that demonstrates the 
problem (run under linux (amd64) using getools v. 14.0) as well as the 
shapefile.

Thanks in advance and best regards!
 Thomas

Attachment: shape_flurstuecke.shx
Description: application/esri-shape-index

Attachment: shape_flurstuecke.shp
Description: application/esri-shape

Attachment: shape_flurstuecke.dbf
Description: application/dbf

package org.geotools.JSurveyMvn;

import java.io.File;
import java.io.IOException;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.referencing.CRS;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swt.SwtMapPane;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;


public class App extends ApplicationWindow {
	private SwtMapPane mappane = null;
	private MapContent map = new MapContent();
	private CoordinateReferenceSystem map_crs = null;
	
    public App(){
    	super( null );
		setBlockOnOpen( true );
		open();
		Display.getCurrent().dispose();
	}

    protected void configureShell( final Shell shell ){
		super.configureShell( shell );
		shell.setText( "gtswtmap testapp" );
		shell.setSize( 1024, 730 );
    }
    
	public static void main( String[] args ) {
		new App();
    }
    
	void fileOpen(){
		FileDialog dlg = new FileDialog( getShell(), SWT.OPEN );
		dlg.setFilterNames( new String[] { "Esri Shape Files (*.shp)" } );
		dlg.setFilterExtensions( new String[] { "*.shp" } );
		String filename = dlg.open();
		if( filename != null && !filename.isEmpty() ){
			System.out.printf( "opening file for new layer '%s'\n", filename );
			File file = new File( filename );
			if( file != null ){
				try{
					FileDataStore store = FileDataStoreFinder.getDataStore( file );
					SimpleFeatureSource feature_source = store.getFeatureSource();
			
					SimpleFeatureCollection sfc = feature_source.getFeatures();
					SimpleFeatureIterator it = sfc.features();
					while( it.hasNext() ){
						SimpleFeature f = it.next();
						System.out.println(  f.getFeatureType());
					}
			
					Style style = SLD.createSimpleStyle( feature_source.getSchema() );
					Layer new_layer = new FeatureLayer( feature_source, style );
					if( null == new_layer.getBounds().getCoordinateReferenceSystem() ){
						System.out.println( "No CRS set for new (shapefile) layer!"  );
					}
					
					map.addLayer( new_layer );
				}catch( IOException ex ){ ex.printStackTrace(); }
			}
		}else{
			System.out.println( "no file chosen or cancelled" );
		}
	}
	
	protected Control createContents( Composite parent ){
		final Shell s = parent.getShell();
 
		Menu mnu = new Menu( s, SWT.BAR );

		MenuItem mnu_file_hdr = new MenuItem( mnu, SWT.CASCADE );
		Menu mnu_file = new Menu( s, SWT.DROP_DOWN );
		mnu_file_hdr.setText( "&File" );
		mnu_file_hdr.setMenu( mnu_file );

		MenuItem mnu_file_open = new MenuItem( mnu_file, SWT.PUSH );
		mnu_file_open.setText( "&Open" );
		mnu_file_open.addSelectionListener( new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent arg0) {
				fileOpen();
			}
			
			public void widgetSelected(SelectionEvent arg0) {
				fileOpen();
			}
		});
		
		MenuItem mnu_setcrs = new MenuItem( mnu_file, SWT.PUSH );
		mnu_setcrs.setText( "&Set CRS (to EPSG5678)" );
		mnu_setcrs.addSelectionListener( new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent arg0) {
				mappane.setCrs( map_crs );
			}
			
			public void widgetSelected(SelectionEvent arg0) {
				mappane.setCrs( map_crs );
			}
		});
		
		MenuItem mnu_file_quit = new MenuItem( mnu_file, SWT.PUSH );
		mnu_file_quit.setText( "&Quit" );
		mnu_file_quit.addSelectionListener( new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent arg0) {
				s.dispose();
			}
			
			public void widgetSelected(SelectionEvent arg0) {
				s.dispose();
			}
		});
		
		s.setMenuBar( mnu );
		
		
		mappane = new SwtMapPane( s, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.NO_BACKGROUND );
		map.setTitle( "JSurvey Map" );
		mappane.setMapContent( map );
		mappane.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		mappane.setRenderer( new StreamingRenderer() );
		
		try{
			map_crs = CRS.decode( "EPSG:5678" );
			mappane.setCrs( map_crs );
		}catch( NoSuchAuthorityCodeException e ){ e.printStackTrace();
		}catch( FactoryException e ){ e.printStackTrace(); }
		
		return( mappane );
	}
}
------------------------------------------------------------------------------
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to