一、读shp文件
public static void readShp(String path){
ShapefileDataStore shpDataStore = null;
try{
shpDataStore = new ShapefileDataStore(new File(path).toURI().toURL());
shpDataStore.setCharset(Charset.forName("GBK"));
String typeName = shpDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
FeatureIterator<SimpleFeature> itertor = result.features();
// FeatureWriter<SimpleFeatureType, SimpleFeature> writer = shpDataStore.getFeatureWriter(shpDataStore.getTypeNames()[0], Transaction.AUTO_COMMIT);
//写下一条
while(itertor.hasNext()){
SimpleFeature feature = itertor.next();
Collection<Property> p = feature.getProperties();
Iterator<Property> it = p.iterator();
System.out.println("fid:"+feature.getID());
System.out.println("geom:"+feature.getDefaultGeometry());
//SimpleFeature featureNew = writer.next();
//featureNew.setAttributes(feature.getAttributes());
while(it.hasNext()) {
Property pro = it.next();
System.out.println(pro.getName() + " = " + pro.getValue());
}
//writer.write();
}
itertor.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) { e.printStackTrace(); }
}
2.写shp文件
public static void write(String filepath,List<Monitor> monitorList) {
try {
//创建shape文件对象
File file = new File(filepath);
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
//定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
tb.add("the_geom", Point.class);
tb.add("fid", String.class);
tb.add("ip", String.class);
tb.add("neghname",String.class);
tb.add("pointname",String.class);
tb.add("mn",String.class);
ds.createSchema(tb.buildFeatureType());
ds.setCharset(Charset.forName("GBK"));
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
//写下一条
SimpleFeature feature;
for(int i=0;i<monitorList.size();i++){
Monitor monitor = monitorList.get(i);
feature = writer.next();
feature.setAttribute("the_geom",monitor.getCoordi() );
feature.setAttribute("fid", monitor.getFid());
feature.setAttribute("ip", monitor.getIp());
feature.setAttribute("neghname", monitor.getNeghname());
feature.setAttribute("pointname", monitor.getPointname());
feature.setAttribute("mn", monitor.getMn());
}
writer.write();
writer.close();
ds.dispose();
//读取刚写完shape文件的图形信息
ShpFiles shpFiles = new ShpFiles(filepath);
ShapefileReader reader = new ShapefileReader(shpFiles, false, true, new GeometryFactory(), false);
try {
while (reader.hasNext()) {
System.out.println(reader.nextRecord().shape());
}
} finally {
reader.close();
}
} catch (Exception e) { }
}
3.copy shp文件
public static void transShape(String srcfilepath, String destfilepath) {
try {
//源shape文件
ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());
//创建目标shape文件对象
Map<String, Serializable> params = new HashMap<String, Serializable>();
FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());
ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
// 设置属性
SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
//下面这行还有其他写法,根据源shape文件的simpleFeatureType可以不用retype,而直接用fs.getSchema设置
ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));
//设置writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
//写记录
SimpleFeatureIterator it = fs.getFeatures().features();
try {
while (it.hasNext()) {
SimpleFeature f = it.next();
SimpleFeature fNew = writer.next();
fNew.setAttributes(f.getAttributes());
fNew.setAttribute(4, getRandomData(33,22)+random());
fNew.setAttribute(5, getRandomData(323,208)+random());
fNew.setAttribute(6, getRandomData(10,0)+random());
fNew.setAttribute(7, getRandomData(1018,1009)+random());
fNew.setAttribute(8, getRandomData(43,12)+random());
fNew.setAttribute(9, getRandomData(45,11)+random());
fNew.setAttribute(10, getRandomData(17,3)+random());
fNew.setAttribute(11, getRandomData(29,8)+random());
fNew.setAttribute(12, getRandomData(85,40)+random());
writer.write();
}
} finally {
it.close();
}
writer.close();
ds.dispose();
shapeDS.dispose();
} catch (Exception e) { e.printStackTrace(); }
}
4.生成特定范围内的随机数
/**
* 生成范围间的整数
* @param max
* @param min
* @return
*/
public static int getRandomData(int max,int min){
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
return s;
}
5.生成0-1间保留一位小数的随机值
/**
* 取只有一位小数的随机值
* @return
*/
public static double random(){
BigDecimal val = new BigDecimal(Math.random());
double v = val.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
return v;
}
6.四舍五入保留一位小数
public static double getOneDoc(double data){
data = Math.round(data*10)/10.0;
return data;
}
参考文章: JAVA用geotools读写shape格式文件