[udig-devel] Problem with writing layer with LineString to shapefile

Ingmar Eveslage eveslage at gmail.com
Fri Mar 28 13:39:27 PDT 2008


Hello,

i have a Problem with writing features to a shapefile. i try the  
following:

i have a map with two layers. the first with a point geometry and one  
with a line string geometry using the following featuretype:

				CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
				AttributeType point =  
AttributeTypeFactory.newAttributeType("GEOMETRY",Point.class,true,  
null,null,crs );
				AttributeType name = AttributeTypeFactory.newAttributeType("Name",  
String.class, false, null, "Station", null);
				stationFeatureType = FeatureTypeBuilder.newFeatureType(new  
AttributeType[] { point, name }, "StationFeatureType");

and

				CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
				AttributeType line =  
AttributeTypeFactory.newAttributeType("GEOMETRY",  
LineString.class,true, null,null,crs );
				AttributeType stationId1 =  
AttributeTypeFactory.newAttributeType("StationID1", String.class,  
false, null, "Station1", null);
				AttributeType stationId2 =  
AttributeTypeFactory.newAttributeType("StationID2", String.class,  
false, null, "Station2", null);	
				AttributeType linkQuality =  
AttributeTypeFactory.newAttributeType("LinkQuality", Double.class,  
false, null, 1.0, null);	
				linkFeatureType = FeatureTypeBuilder.newFeatureType(new  
AttributeType[] { line }, "LinkFeatureType");

after editing the layers using the creation tool from udig a want to  
save the features on the disk in a shapefile using this method:

	private void writeFeatureSourceToShapefile(FeatureSource source) {
		try {
			FeatureType ft = source.getSchema();
			String ftName = ft.getTypeName();
			CoordinateReferenceSystem crs =  
ft.getDefaultGeometry().getCoordinateSystem();

			IFolder directory =  
Activator.getDefault().getMyWebProject().getFolder(getName() +  
DIRECTORY_EXTENSION);
			if (!directory.exists())
				directory.create(true, true, null);
			
			IFile file = directory.getFile(ftName + SHAPEFILE_EXTENSION);
			
			Map params = new HashMap();
			params.put("shapefile url", file.getLocationURI().toURL());

			FileDataStoreFactorySpi factory = new  
IndexedShapefileDataStoreFactory();
			ShapefileDataStore dataStore = (ShapefileDataStore)  
factory.createNewDataStore(params);
			dataStore.createSchema(ft);
			dataStore.forceSchemaCRS( crs );
			
			FeatureStore featureStore = (FeatureStore)  
dataStore.getFeatureSource();

			Transaction t = new DefaultTransaction();
			try {
				FeatureCollection collection = source.getFeatures(); // grab all  
features
				featureStore.addFeatures( collection );
				t.commit(); // write it out
			}
			catch( IOException eek){
				eek.printStackTrace();
				try {
					t.rollback();
				}
				catch( IOException doubleEeek ){
					// rollback failed?
				}
			}
			finally {
				t.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

all works fine with the stationFeatureType. I get a shapefile with all  
the features. But if i do the same with the linkFeatureType i get this  
exception:

java.lang.NullPointerException
	at org.geotools.data.shapefile.indexed.IndexedShapefileDataStore 
$Writer.write(IndexedShapefileDataStore.java:1752)
	at org.geotools.data.shapefile.indexed.IndexedShapefileDataStore 
$Writer.close(IndexedShapefileDataStore.java:1557)
	at org.geotools.data.InProcessLockingManager 
$1.close(InProcessLockingManager.java:344)
	at  
org 
.geotools 
.data.AbstractFeatureStore.addFeatures(AbstractFeatureStore.java:265)
	at  
de 
.hub 
.sam 
.es.editor.model.Network.writeFeatureSourceToShapefile(Network.java:367)
	at de.hub.sam.es.editor.model.Network.exportToShapefile(Network.java: 
334)
	at de.hub.sam.es.editor.view.StationList.dispose(StationList.java:87)
	at  
org 
.eclipse 
.ui 
.internal 
.WorkbenchPartReference.doDisposePart(WorkbenchPartReference.java:720)
	at  
org.eclipse.ui.internal.ViewReference.doDisposePart(ViewReference.java: 
127)
	at  
org 
.eclipse 
.ui 
.internal.WorkbenchPartReference.dispose(WorkbenchPartReference.java: 
671)
	at  
org.eclipse.ui.internal.WorkbenchPage.disposePart(WorkbenchPage.java: 
1604)
	at  
org.eclipse.ui.internal.WorkbenchPage.partRemoved(WorkbenchPage.java: 
1596)
	at org.eclipse.ui.internal.ViewFactory.releaseView(ViewFactory.java: 
255)
	at org.eclipse.ui.internal.Perspective.hideView(Perspective.java:558)
	at org.eclipse.ui.internal.WorkbenchPage.hideView(WorkbenchPage.java: 
2270)
	at org.eclipse.ui.internal.ViewPane.doHide(ViewPane.java:212)
	at org.eclipse.ui.internal.PartStack.close(PartStack.java:543)
	at org.eclipse.ui.internal.PartStack.close(PartStack.java:526)
	at org.eclipse.ui.internal.PartStack$1.close(PartStack.java:122)
	at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation 
$1.handleEvent(TabbedStackPresentation.java:81)
	at  
org 
.eclipse 
.ui 
.internal 
.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java: 
267)
	at  
org 
.eclipse 
.ui 
.internal 
.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java: 
276)
	at org.eclipse.ui.internal.presentations.r33.DefaultTabFolder.access 
$1(DefaultTabFolder.java:1)
	at org.eclipse.ui.internal.presentations.r33.DefaultTabFolder 
$1.closeButtonPressed(DefaultTabFolder.java:66)
	at  
org 
.eclipse 
.ui 
.internal 
.presentations.r33.PaneFolder.notifyCloseListeners(PaneFolder.java:601)
	at org.eclipse.ui.internal.presentations.r33.PaneFolder 
$3.close(PaneFolder.java:190)
	at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:2159)
	at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:320)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3682)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3293)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2389)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2219)
	at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
	at  
org 
.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java: 
289)
	at  
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java: 
461)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
	at net.refractions.udig.internal.ui.UDIGApplication.start(Unknown  
Source)
	at  
org 
.eclipse 
.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:169)
	at  
org 
.eclipse 
.core 
.runtime 
.internal 
.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)
	at  
org 
.eclipse 
.core 
.runtime 
.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)
	at  
org 
.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java: 
363)
	at  
org 
.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java: 
176)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:508)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1148)

Can anyone tell me whats the problem? i dont know why this does not  
work with the LineString class.

Thanks for your help.

Greats Ingmar


More information about the udig-devel mailing list