geotools和geoserver利用WFS-T通信,insert操作
本人最近需要利用java平台和geoserver发布的wfs服务通信,开始学习geotools中间件
其中,在学习wfs-t服务是总是报空指针的错误,哪位大哥能不吝赐教下~
具体代码:
package wfsTest;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.wfs.v1_0_0.WFSFeatureStore;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.FeatureType;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
public class WFS_T_Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
//连接geoserver
String getCapabilities = "http://localhost:8085/geoserver/ows?service=wfs&version=1.0.0&request=GetCapabilities";
Map connectionParameters = new HashMap();
//获取要素
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities );
DataStore ds = DataStoreFinder.getDataStore(connectionParameters);
FeatureType ft = ds.getSchema("sf:archsites");
WFSFeatureStore fs= (WFSFeatureStore)ds.getFeatureSource("sf:archsites");
Transaction insert = new DefaultTransaction("insert");
fs.setTransaction(insert);
//创建一个feature对象
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
Point point = geometryFactory.createPoint(new Coordinate(32.0600, 118.8563));
SimpleFeatureBuilder simpleFeatureBuilder = new SimpleFeatureBuilder((SimpleFeatureType)ft);
simpleFeatureBuilder.add(point);
simpleFeatureBuilder.add(27);
simpleFeatureBuilder.add("testName111");
SimpleFeature feature = simpleFeatureBuilder.buildFeature(null);
/* SimpleFeatureCollection fc = FeatureCollections.newCollection();
fc.add(feature);
*/
//添加数据并提交事务
//fs.addFeatures(new CollectionFeatureReader(new SimpleFeature[]{feature}));
//fs.addFeatures(fc);
fs.addFeatures( DataUtilities.collection( feature ) );
insert.commit();
}
}
错误信息:Exception in thread "main" java.lang.NullPointerException
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at org.geotools.data.wfs.v1_0_0.WFSTransactionState.commitPost(WFSTransactionState.java:261)
at org.geotools.data.wfs.v1_0_0.WFSTransactionState.commit(WFSTransactionState.java:152)
at org.geotools.data.DefaultTransaction.commit(DefaultTransaction.java:182)
at wfsTest.WFS_T_Test.main(WFS_T_Test.java:78)