GeoTools 简单记录
选择19.0版本:https://sourceforge.net/projects/geotools/files/GeoTools%2019%20Releases/19.0/
主要是记录一:shp文件存在还是不存在调用的方法
// 判断文件是否存在
public void judeFileExists() throws Exception {
// 1.创建shape文件对象
File file = new File(filepath_myPoint);
if (!file.exists()) {
System.out.println("***********文件不存在***********");
Map<String, Serializable> params = new HashMap<>();
// 用于捕获参数需求的数据类
// URLP:url to the .shp file.
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
// 2.创建一个新的数据存储——对于一个还不存在的文件。
shapefileDataStore = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
// 3.定义图形信息和属性信息
// SimpleFeatureTypeBuilder 构造简单特性类型的构造器
final SimpleFeatureType TYPE = createFeatureType();
// 设置此数据存储的特征类型
shapefileDataStore.createSchema(TYPE);
// 设置编码,防止中文读取乱码
shapefileDataStore.setCharset(Charset.forName("UTF-8"));
} else {
System.out.println("**************文件已存在***********");
// 构建一个已存在的shapfile数据源
// ShapefileDataStore:数据存储实现,允许从Shapefiles读取和写入
shapefileDataStore = (ShapefileDataStore) new ShapefileDataStoreFactory()
.createDataStore(file.toURI().toURL());
// 设置编码,防止中文读取乱码
shapefileDataStore.setCharset(Charset.forName("UTF-8"));
}
}
记录二:如果想在一个shp文件中添加一个动点,动点的取值范围必须在shp文件比例尺范围内
/**
* 1.1根据X,Y坐标构建一个几何对象: 点 【Point】
// System.out.println("X: " + layer.getBounds().getMinX() + " ~ " + layer.getBounds().getMaxX());
// xmin = layer.getBounds().getMinX();
// xmax = layer.getBounds().getMaxX();
// System.out.println("Y: " + layer.getBounds().getMinY() + " ~ " + layer.getBounds().getMaxY());
// ymin = layer.getBounds().getMinY();
// ymax = layer.getBounds().getMaxY();
* @param x
* @param y
* @return
*/
public Point createPoint(){
double x, y;
x = xmin + ((xmax - xmin) * new Random().nextDouble());
y = ymin + ((ymax - ymin) * new Random().nextDouble());
// 保留五位小数
String x_str = decimalFormat.format(x);
String y_str = decimalFormat.format(y);
Coordinate coord = new Coordinate(Double.valueOf(x_str),Double.valueOf(y_str));
Point point = geometryFactory.createPoint(coord);
return point;
}
记录三:动点样式【是一个自定义小图片】
/**
* 设置动点是图片样式
* @return
* @throws MalformedURLException
*/
private Style createPoint_GraphicsBasedStyle() throws MalformedURLException {
StyleBuilder sb = new StyleBuilder();
String filePath=new File(System.getProperty("user.dir")+"/images").getAbsolutePath();
String iconPath = filePath+"\\XXXX.png";
URL url=new File(iconPath).toURI().toURL();
ExternalGraphic icon = sb.createExternalGraphic(url, "image/png");
// Graphic graph2 = sb.createGraphic(null, circle, null, 1.0, 4.0, 0.0); //使用内部已知图元
Graphic graph2 = sb.createGraphic(icon, null, null, 1.0, 26.0, 0.0); //使用外部图片
PointSymbolizer symbolizer = sb.createPointSymbolizer(graph2);
symbolizer.getGraphic().setSize(filterFactory2.literal(30)); //设置大小
Rule rule = sb.createRule(symbolizer);
FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[]{rule});
Style style = styleFactory.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
转载自:https://blog.csdn.net/ysblogs/article/details/80756984