geoTools向shp文件中写数据

来源:互联网 发布:手机迅雷提示无网络 编辑:程序博客网 时间:2024/05/16 15:56
package test;import java.io.File;import java.io.Serializable;import java.util.HashMap;import java.util.Map;import org.geotools.data.DataUtilities;import org.geotools.data.DefaultTransaction;import org.geotools.data.Transaction;import org.geotools.data.shapefile.ShapefileDataStore;import org.geotools.data.shapefile.ShapefileDataStoreFactory;import org.geotools.data.simple.SimpleFeatureCollection;import org.geotools.data.simple.SimpleFeatureSource;import org.geotools.data.simple.SimpleFeatureStore;import org.geotools.feature.FeatureCollections;import org.geotools.feature.simple.SimpleFeatureBuilder;import org.geotools.referencing.crs.DefaultGeographicCRS;import org.junit.Test;import org.opengis.feature.simple.SimpleFeature;import org.opengis.feature.simple.SimpleFeatureType;import com.vividsolutions.jts.geom.Coordinate;import com.vividsolutions.jts.geom.GeometryFactory;import com.vividsolutions.jts.geom.Point;//使用geotools2.7.2public class WriteData {    @Test    public void write2() {          try{                //定义属性              final SimpleFeatureType TYPE = DataUtilities.createType("Location",                  "location:Point," + // <- the geometry attribute: Point type                  "POIID:String," + // <- a String attribute                  "MESHID:String," + // a number attribute                  "OWNER:String"              );              SimpleFeatureCollection collection = FeatureCollections.newCollection();              GeometryFactory geometryFactory = new GeometryFactory();              SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);                    double latitude = Double.parseDouble("116.123456789");              double longitude = Double.parseDouble("39.120001");              String POIID = "2050003092";              String MESHID = "0";              String OWNER = "340881";              Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));              Object[] obj = {point, POIID, MESHID, OWNER};              SimpleFeature feature = featureBuilder.buildFeature(null, obj);              collection.add(feature);              feature = featureBuilder.buildFeature(null, obj);              collection.add(feature);              File newFile = new File("D:/newPoi.shp");              ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();              Map<String, Serializable> params = new HashMap<String, Serializable>();              params.put("url", newFile.toURI().toURL());              params.put("create spatial index", Boolean.TRUE);              ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);              newDataStore.createSchema(TYPE);              newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);                    Transaction transaction = new DefaultTransaction("create");              String typeName = newDataStore.getTypeNames()[0];              SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);                    if (featureSource instanceof SimpleFeatureStore) {                  SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;                  featureStore.setTransaction(transaction);                  try {                      featureStore.addFeatures(collection);                      transaction.commit();                  } catch (Exception problem) {                      problem.printStackTrace();                  transaction.rollback();                  } finally {                      transaction.close();                  }              } else {                  System.out.println(typeName + " does not support read/write access");              }          } catch (Exception e) {              e.printStackTrace();          }      }  }

0 0
原创粉丝点击