java使用GeoTools讀取shp文件并畫圖的操作代碼
GeoTools是ArcGis地圖與java對象的橋梁,恰如jdbc之于oracle與java。
shp文件本身是存有地理對象邊界坐標(biāo)、對象中心城市及城市編號的多多邊形字符串。
需要使用的依賴如下
<!-- 添加GeoTools依賴 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
對象:
public class CutProperty {
private String province;
private int x;
private int y;
/**
* 圖片寬
* */
private int width;
/**
* 圖片高
* */
private int height;
/**
* 畫圖時線條粗細(xì)
* */
private float weight;
/**
* 地圖坐標(biāo)右邊界
* */
private int rightborder;
/**
* 地圖坐標(biāo)放大倍數(shù)
* */
private int bei;
/**
* 文字大小
* */
private int stringsize;
來自@author yukun24@126.com的工具類,讀取shp文件:
package com.gwhn.geotools;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.Test;
import org.locationtech.jts.geom.*;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.*;
public class ShapeUtil {
//讀shp文件【幾何信息+屬性信息】
public List<Property> SHPRead(String path) throws Exception {
List<Property> propertyList = new ArrayList<>();
//基于上面新建的shapfile文件,進行讀取
//構(gòu)建shapefile數(shù)據(jù)存儲的實例
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
//基于路徑構(gòu)建文件對象
File file = new File(path);
//構(gòu)建一個已存在的shapfile數(shù)據(jù)源
//ShapefileDataStore:數(shù)據(jù)存儲實現(xiàn),允許從Shapefiles讀取和寫入
ShapefileDataStore ds = (ShapefileDataStore) dataStoreFactory.createDataStore(file.toURI().toURL());
//設(shè)置編碼,防止中文讀取亂碼
ds.setCharset(Charset.forName("UTF-8"));
//getFeatureSource():ContentFeatureSource
//這個特性是由 FeatureCollection提供的操作完成的。單獨的特征記憶實現(xiàn)由子類提供:
//SimpleFeatureSource特征資源明確地使用FeatureCollection【集合】,可迭代
SimpleFeatureSource featureSource = ds.getFeatureSource();
//getFeatures():以FeatureCollection的形式檢索所有特性。
//一個用于處理FeatureCollection的實用工具類。提供一個獲取FeatureCollection實例的機制
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
System.out.println("幾何對象總共有:" + result.size());
//features():返回一個FeatureIterator迭代器
SimpleFeatureIterator it = (SimpleFeatureIterator) result.features();
while (it.hasNext()) {
SimpleFeature feature = it.next();
//迭代屬性【屬性我們可以理解為一個幾何對象的屬性節(jié)點,也就是對一個幾何圖形的描述字段】
Iterator<Property> ip = feature.getProperties().iterator();
// System.out.println("========================");
//再來個while
while (ip.hasNext()) {
Property pro = ip.next();
// System.out.println(pro.getName()+" = "+pro.getValue());
propertyList.add(pro);
}//end 屬性迭代
}
it.close();
return propertyList;
}
}
來自@author yukun24@126.com的工具類,此處用來將shp中獲取的字符串轉(zhuǎn)化為多多邊形對象MultiPolygon
package com.gwhn.geotools;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import java.util.List;
/**
* 幾何對象構(gòu)建器
*
* @author yukun24@126.com
* @version V1.0.1
* @blob http://blog.csdn.net/appleyk
* @date 2017年12月8日10:38:49
*/
//單例模式
public class GeometryCreator {
public static GeometryCreator geometryCreator = null;
private GeometryFactory geometryFactory = new GeometryFactory();
public GeometryCreator() {
}
/**
* 返回本類的唯一實例
*
* @return
*/
public static GeometryCreator getInstance() {
if (geometryCreator == null) {
return new GeometryCreator();
}
return geometryCreator;
}
/**
* 1.構(gòu)建點
*/
/**
* 1.1根據(jù)X,Y坐標(biāo)構(gòu)建一個幾何對象: 點 【Point】
*
* @param x
* @param y
* @return
*/
public Point createPoint(double x, double y) {
Coordinate coord = new Coordinate(x, y);
Point point = geometryFactory.createPoint(coord);
return point;
}
/**
* 1.2根據(jù)幾何對象的WKT描述【String】創(chuàng)建幾何對象: 點 【Point】
*
* @return
* @throws ParseException
*/
public Point createPointByWKT(String PointWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
Point point = (Point) reader.read(PointWKT);
return point;
}
/**
* 1.3根據(jù)幾何對象的WKT描述【String】創(chuàng)建幾何對象:多點 【MultiPoint】
*
* @return
* @throws ParseException
*/
public MultiPoint createMulPointByWKT(String MPointWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
MultiPoint mpoint = (MultiPoint) reader.read(MPointWKT);
return mpoint;
}
/**
* 2.構(gòu)建線
*/
/**
* 2.1根據(jù)兩點 創(chuàng)建幾何對象:線 【LineString】
*
* @param ax
* @param ay
* @param bx
* @param by
* @return
*/
public LineString createLine(double ax, double ay, double bx, double by) {
Coordinate[] coords = new Coordinate[]{new Coordinate(ax, ay), new Coordinate(bx, by)};
LineString line = geometryFactory.createLineString(coords);
return line;
}
/**
* 2.2根據(jù)線的WKT描述創(chuàng)建幾何對象:線 【LineString】
*
* @param LineStringWKT
* @return
* @throws ParseException
*/
public LineString createLineByWKT(String LineStringWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");
return line;
}
/**
* 2.3根據(jù)點組合的線數(shù)組,創(chuàng)建幾何對象:多線 【MultiLineString】
*
* @param list
* @return
*/
public MultiLineString createMLine(List<Coordinate[]> list) {
MultiLineString ms = null;
if (list == null) {
return ms;
}
LineString[] lineStrings = new LineString[list.size()];
// Coordinate[] coords1 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
// LineString line1 = geometryFactory.createLineString(coords1);
//
// Coordinate[] coords2 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
// LineString line2 = geometryFactory.createLineString(coords2);
int i = 0;
for (Coordinate[] coordinates : list) {
lineStrings[i] = geometryFactory.createLineString(coordinates);
}
ms = geometryFactory.createMultiLineString(lineStrings);
return ms;
}
/**
* 2.4根據(jù)幾何對象的WKT描述【String】創(chuàng)建幾何對象 : 多線【MultiLineString】
*
* @param MLineStringWKT
* @return
* @throws ParseException
*/
public MultiLineString createMLineByWKT(String MLineStringWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
MultiLineString line = (MultiLineString) reader.read(MLineStringWKT);
return line;
}
/**
* 3.構(gòu)建多邊形
*/
/**
* 3.1 根據(jù)幾何對象的WKT描述【String】創(chuàng)建幾何對象:多邊形 【Polygon】
*
* @param PolygonWKT
* @return
* @throws ParseException
*/
public Polygon createPolygonByWKT(String PolygonWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
Polygon polygon = (Polygon) reader.read(PolygonWKT);
return polygon;
}
/**
* 3.2 根據(jù)幾何對象的WKT描述【String】創(chuàng)建幾何對象: 多多邊形 【MultiPolygon】
*
* @param MPolygonWKT
* @return
* @throws ParseException
*/
public MultiPolygon createMulPolygonByWKT(String MPolygonWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
MultiPolygon mpolygon = (MultiPolygon) reader.read(MPolygonWKT);
return mpolygon;
}
/**
* 根據(jù)多邊形數(shù)組 進行多多邊形的創(chuàng)建
*
* @param polygons
* @return
* @throws ParseException
*/
public MultiPolygon createMulPolygonByPolygon(Polygon[] polygons) throws ParseException {
return geometryFactory.createMultiPolygon(polygons);
}
/**
* 4.構(gòu)建幾何對象集合
*/
/**
* 4.1 根據(jù)幾何對象數(shù)組,創(chuàng)建幾何對象集合:【GeometryCollection】
*
* @return
* @throws ParseException
*/
public GeometryCollection createGeoCollect(Geometry[] geoArray) throws ParseException {
// LineString line = createLine(125.12,25.4,85.63,99.99);
// Polygon poly = createPolygonByWKT("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
// Geometry g1 = geometryFactory.createGeometry(line);
// Geometry g2 = geometryFactory.createGeometry(poly);
// Geometry[] geoArray = new Geometry[]{g1,g2};
GeometryCollection gc = geometryFactory.createGeometryCollection(geoArray);
return gc;
}
/**
* 5.構(gòu)建圓
*/
/**
* 5.1 根據(jù)圓點以及半徑創(chuàng)建幾何對象:特殊的多邊形--圓 【Polygon】
*
* @param x
* @param y
* @param RADIUS
* @return
*/
public Polygon createCircle(double x, double y, final double RADIUS) {
final int SIDES = 32;//圓上面的點個數(shù)
Coordinate coords[] = new Coordinate[SIDES + 1];
for (int i = 0; i < SIDES; i++) {
double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
double dx = Math.cos(angle) * RADIUS;
double dy = Math.sin(angle) * RADIUS;
coords[i] = new Coordinate((double) x + dx, (double) y + dy);
}
coords[SIDES] = coords[0];
//線性環(huán)
LinearRing ring = geometryFactory.createLinearRing(coords);
Polygon polygon = geometryFactory.createPolygon(ring, null);
return polygon;
}
/**
* 6.構(gòu)建環(huán)
*/
/**
* 6.1 根據(jù)WKT創(chuàng)建環(huán)
*
* @param ringWKT
* @return
* @throws ParseException
*/
public LinearRing createLinearRingByWKT(String ringWKT) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
LinearRing ring = (LinearRing) reader.read(ringWKT);
return ring;
}
}
有xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<!-- 網(wǎng)絡(luò)配置-->
<module name="Http">
<group name="FileExport">
<configValue key="ShapePath">D:/testspace/spd/rain/</configValue>
<configValue key="ProvinceShapePath">D:/testspace/spd/Provinces_shp/</configValue>
<configValue key="ProvincePath">D:\testspace\spd\output\</configValue>
<configValue key="ExportPath">D:\testspace\spd\Export\</configValue>
<configValue key="PropertyPath">D:\testspace\spd\Export\</configValue>
</group>
</module>
</application>
xml讀取以獲取shp文件位置和圖片輸出位置:
package com.gwhn.Util;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
/**
* @author banxian
* @date 2021/1/21 15:56
*/
public class YereeXmlContext {
/**
* 根據(jù)一二三級字節(jié)點的key,迭代到第三級節(jié)點根據(jù)三級key獲取值
* <!-- 網(wǎng)絡(luò)配置-->
* <module name="Http">
* <group name="FileUpload">
* <configValue key="TempDir">D:\\temp\\upload\\</configValue>
* </group>
* </module>
*
* @param key1
* @param key2
* @param key3
* @return
* @author banxian
* @date: 2021/1/21 19:51
*/
public String getXmlValue(String key1, String key2, String key3) {
try {
//1.創(chuàng)建Reader對象
SAXReader reader = new SAXReader();
//2.加載xml
String directoryPath = Thread.currentThread().getContextClassLoader().getResource("config.xml").getPath();//獲取項目根目錄
Document document = reader.read(new File(directoryPath));
//3.獲取根節(jié)點
Element rootElement = document.getRootElement();
Element moduleElement = getTargetElement(key1, rootElement);
Element groupElement = getTargetElement(key2, moduleElement);
Element configElement = getTargetElement(key3, groupElement);
Attribute attribute = (Attribute) configElement.attributes().get(0);
String key = attribute.getValue();
Text text3 = (Text) configElement.content().get(0);
String value = text3.getText();
return value;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 獲取和targetKey匹配的一個子節(jié)點
*
* @param targetKey
* @param parentElement
* @return
* @author banxian
* @date: 2021/1/21 19:53
*/
public Element getTargetElement(String targetKey, Element parentElement) {
Iterator iterator = parentElement.elementIterator();
Element childElement = null;
while (iterator.hasNext()) {
Element element = (Element) iterator.next();
Attribute attribute = (Attribute) element.attributes().get(0);
String key = attribute.getValue();
if (key.equals(targetKey) || key == targetKey) {
childElement = element;
break;
}
}
return childElement;
}
}
核心類:用多邊形繪圖
package com.gwhn.Util;
import com.gwhn.entity.CutProperty;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPolygon;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
/**
* @author banxian
* @date 2021/6/28 12:37
*/
public class PolygonUtil {
ImageUtils imageUtils = new ImageUtils();
TxtUtil txtUtil = new TxtUtil();
/**
* 根據(jù)多多邊形畫圖
* @param path
* @param cutProperty
* @param multiPolygonList
* @return
* @author banxian
* @date: 2021/6/30 15:31
*/
public void graphicsByMultiPolygon(String path, List<MultiPolygon> multiPolygonList, CutProperty cutProperty) {
try {
int imageWidth = cutProperty.getWidth();// 圖片的寬度
int imageHeight = cutProperty.getHeight();// 圖片的高度
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.white);
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(Color.black);
int m = cutProperty.getBei() / 100;
graphics.setStroke(new BasicStroke(cutProperty.getWeight() * m));//線條粗細(xì)
Font font = new Font("宋體", Font.BOLD, 1200);
graphics.setFont(font);
graphics.drawString(cutProperty.getProvince(), 12500, 1500);
for (MultiPolygon multiPolygon : multiPolygonList) {
int num = multiPolygon.getNumGeometries();
for (int i = 0; i < num; i++) {
Geometry geometry = multiPolygon.getGeometryN(i);
drawPolygon(geometry, cutProperty, graphics);
}
}
imageUtils.createImage(path, image);
System.out.println(path + ":" + font.getSize());
} catch (
Exception e) {
e.printStackTrace();
}
}
/**
* 畫出一個多邊形
* @param geometry
* @param cutProperty
* @param graphics2D
* @return
* @author banxian
* @date: 2021/6/28 12:37
*/
public void drawPolygon(Geometry geometry, CutProperty cutProperty, Graphics2D graphics2D) {
int m = cutProperty.getBei() / 100;
Double xtemp = Double.valueOf(cutProperty.getX() * m);
Double ytemp = Double.valueOf(cutProperty.getY() * m);
//處理飛地
if (geometry.toString().contains("),")) {
String geoStr = geometry.toString();
String tempStr = geoStr.substring(9, geoStr.length() - 1) + ",";
while (tempStr.contains("),")) {
String temp = tempStr.substring(0, tempStr.indexOf("),") + 2);
tempStr = tempStr.substring(temp.length()).trim();
Polygon polygon = txtUtil.getPolygonByStr(temp,cutProperty);
graphics2D.drawPolygon(polygon);
}
} else {
Coordinate[] coordinates = geometry.getCoordinates();
Polygon polygon = new Polygon();
Double x, y;
for (int j = 0; j < coordinates.length; j++) {
Coordinate coordinate = coordinates[j];
x = coordinate.getX() * cutProperty.getBei() - xtemp;
y = cutProperty.getRightborder() * m - coordinate.getY() * cutProperty.getBei() - ytemp;
polygon.addPoint(x.intValue(), y.intValue());
}
graphics2D.drawPolygon(polygon);
}
}
}
工具類:處理特殊的邊界,一般由飛地、島嶼組成
package com.gwhn.Util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gwhn.entity.CutProperty;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author banxian
* @date 2021/5/10 15:57
*/
public class TxtUtil {
public Polygon getPolygonByStr(String polygonStr,CutProperty cutProperty) {
polygonStr = polygonStr.substring(1, polygonStr.length() - 2) + ",";
List<String> polygonStrList = new ArrayList<>();
while (polygonStr.contains(",")) {
String pointStr = polygonStr.substring(0, polygonStr.indexOf(",")).trim();
polygonStrList.add(pointStr);
polygonStr = polygonStr.substring(polygonStr.indexOf(",") + 1);
}
return transPointStrToPolygon(polygonStrList, cutProperty);
}
public Polygon transPointStrToPolygon(List<String> polygonStrList,CutProperty cutProperty) {
int m = cutProperty.getBei() / 100;
Double xtemp = Double.valueOf(cutProperty.getX() * m);
Double ytemp = Double.valueOf(cutProperty.getY() * m);
Polygon polygon = new Polygon();
for (String pointStr : polygonStrList) {
Double x = Double.parseDouble(pointStr.substring(0, pointStr.indexOf(" ")));
Double y = Double.parseDouble(pointStr.substring(pointStr.indexOf(" ")).trim());
x = x * cutProperty.getBei() - xtemp;
y = cutProperty.getRightborder()*m - y * cutProperty.getBei()-ytemp;
polygon.addPoint(x.intValue(), y.intValue());
}
return polygon;
}
//讀取json文件
public String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 讀取json獲取圖片剪切參數(shù)
*
* @param
* @return
* @author banxian
* @date: 2021/6/25 8:16
*/
public Map<String, CutProperty> getCutPropertyMap() {
String path = Thread.currentThread().getContextClassLoader().getResource("property.json").getPath();//獲取項目根目錄
String s = readJsonFile(path);
JSONObject jsonObject = JSON.parseObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("properties");//構(gòu)建JSONArray數(shù)組
List<CutProperty> cutPropertyList = jsonArray.toJavaList(CutProperty.class);
Map<String, CutProperty> cutPropertyMap = new HashMap();
for (CutProperty cutProperty : cutPropertyList) {
cutPropertyMap.put(cutProperty.getProvince(), cutProperty);
}
return cutPropertyMap;
}
}
核心類:從讀到的shp文件數(shù)據(jù)中提取字符串轉(zhuǎn)化為多多邊形,繪圖
package com.gwhn.controller;
import com.gwhn.Util.PolygonUtil;
import com.gwhn.Util.TxtUtil;
import com.gwhn.Util.YereeXmlContext;
import com.gwhn.entity.CutProperty;
import com.gwhn.geotools.GeometryCreator;
import com.gwhn.geotools.ShapeUtil;
import org.locationtech.jts.geom.MultiPolygon;
import org.opengis.feature.Property;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author banxian
* @date 2021/6/1 8:12
*/
public class ShpPictureController {
PolygonUtil polygonUtil = new PolygonUtil();
TxtUtil txtUtil = new TxtUtil();
Map<String, CutProperty> cutPropertyMap = txtUtil.getCutPropertyMap();
/**
* 根據(jù)國家shp文件畫出中國地圖
* @param
* @return
* @author banxian
* @date: 2021/6/28 12:35
*/
public void doNation() {
YereeXmlContext yereeXmlContext = new YereeXmlContext();
GeometryCreator geometryCreator = new GeometryCreator();
String shapePath = yereeXmlContext.getXmlValue("Http", "FileExport", "ProvincePath") + "province.shp";
ShapeUtil shapeUtil = new ShapeUtil();
try {
List<Property> propertyList = shapeUtil.SHPRead(shapePath);
List<MultiPolygon> multiPolygonList = new ArrayList<>();
for (int i = 0; i * 8 < propertyList.size(); i++) {
Property property = propertyList.get(i * 8);
String valueStr = property.getValue().toString();
MultiPolygon multiPolygon = geometryCreator.createMulPolygonByWKT(valueStr);
multiPolygonList.add(multiPolygon);
}
String path = yereeXmlContext.getXmlValue("Http", "FileExport", "ExportPath");
File newFileDir = new File(path);
//如果不存在 則創(chuàng)建
if (!newFileDir.exists()) {
newFileDir.mkdirs();
}
// String picturName = imageUtils.genImageName() + ".jpg";
String picturName = "中國.jpg";
CutProperty cutProperty = cutPropertyMap.get("中國");
polygonUtil.graphicsByMultiPolygon(path + picturName, multiPolygonList, cutProperty);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("完成");
}
/**
* 根據(jù)分省shp文件畫出地圖
* @param
* @return
* @author banxian
* @date: 2021/6/28 12:35
*/
public void doProvince() {
YereeXmlContext yereeXmlContext = new YereeXmlContext();
GeometryCreator geometryCreator = new GeometryCreator();
ShapeUtil shapeUtil = new ShapeUtil();
String shapeDirPath = yereeXmlContext.getXmlValue("Http", "FileExport", "ProvinceShapePath");
List<File> fileList = new ArrayList<>();
fileList = getFileList(shapeDirPath, fileList);
for (int j = 0; j < fileList.size(); j++) {
File file = fileList.get(j);
if (file.getName().contains(".shp")) {
File parentFile = file.getParentFile();
String province = parentFile.getName();
System.out.println(province);
int size = 7;
if (province.equals("湖北") || province == "湖北" || province.equals("安徽") || province == "安徽") {
size = 10;
}
CutProperty cutProperty = cutPropertyMap.get(province);
try {
List<Property> propertyList = shapeUtil.SHPRead(file.getPath());
List<MultiPolygon> multiPolygonList = new ArrayList<>();
for (int i = 0; i * size < propertyList.size(); i++) {
String valueStr = propertyList.get(i * size).getValue().toString();
MultiPolygon multiPolygon = geometryCreator.createMulPolygonByWKT(valueStr);
multiPolygonList.add(multiPolygon);
}
String path = yereeXmlContext.getXmlValue("Http", "FileExport", "ExportPath");
;
File newFileDir = new File(path);
//如果不存在 則創(chuàng)建
if (!newFileDir.exists()) {
newFileDir.mkdirs();
}
String picturName = province + ".jpg";
polygonUtil.graphicsByMultiPolygon(path + picturName, multiPolygonList, cutProperty);
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.out.println("完成");
}
/**
* 獲取shp文件集合
* @param dirPath
* @param fileList
* @return
* @author banxian
* @date: 2021/6/28 12:36
*/
public List<File> getFileList(String dirPath, List<File> fileList) {
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
for (File file : dir.listFiles()) {
if (!file.isDirectory()) {
fileList.add(file);
} else {
getFileList(file.getPath(), fileList);
}
}
return fileList;
}
}
地圖坐標(biāo)截取參數(shù)
{
"properties": [
{
"province": "中國",
"x": 8000,
"y": 18600,
"width": 25000,
"height": 15000,
"weight": 5.0,
"bei":350,
"rightborder": 25000,
"stringsize": 500
},
{
"province": "上海",
"x": 12050,
"y": 21800,
"width": 23000,
"height": 16000,
"weight": 0.8,
"bei": 10000,
"rightborder": 25000,
"stringsize": 500
},
{
"province": "云南",
"x": 9700,
"y": 22000,
"width": 25000,
"height": 25000,
"weight": 2.0,
"bei": 2500,
"rightborder": 25000,
"stringsize": 50
},
{
"province": "內(nèi)蒙古",
"x": 9000,
"y": 19000,
"width": 25000,
"height": 15000,
"weight": 3.0,
"bei": 600,
"rightborder": 25000,
"stringsize": 180
},
{
"province": "北京",
"x": 11515,
"y": 20830,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 10000,
"rightborder": 25000,
"stringsize": 600
},
{
"province": "吉林",
"x": 12100,
"y": 20200,
"width": 25000,
"height": 17000,
"weight": 2.0,
"bei": 2100,
"rightborder": 25000,
"stringsize": 40
},
{
"province": "四川",
"x": 9600,
"y": 21500,
"width": 25000,
"height": 20000,
"weight": 2.0,
"bei": 1900,
"rightborder": 25000,
"stringsize": 40
},
{
"province": "天津",
"x": 11600,
"y": 20920,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 10000,
"rightborder": 25000,
"stringsize": 600
},
{
"province": "寧夏",
"x": 10400,
"y": 20960,
"width": 20000,
"height": 25000,
"weight": 1.0,
"bei": 4210,
"rightborder": 25000,
"stringsize": 40
},
{
"province": "安徽",
"x": 11350,
"y": 21500,
"width": 25000,
"height": 22000,
"weight": 1.0,
"bei": 3300,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "山東",
"x": 11430,
"y": 21050,
"width": 25000,
"height": 20000,
"weight": 1.0,
"bei": 2900,
"rightborder": 25000,
"stringsize": 35
},
{
"province": "山西",
"x": 10900,
"y": 20850,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 3500,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "廣東",
"x": 10750,
"y": 22400,
"width": 25000,
"height": 15000,
"weight": 1.0,
"bei": 2200,
"rightborder": 25000,
"stringsize": 40
},
{
"province": "廣西",
"x": 10400,
"y": 22200,
"width": 25000,
"height": 18000,
"weight": 1.0,
"bei": 2600,
"rightborder": 25000,
"stringsize": 35
},
{
"province": "新疆",
"x": 7200,
"y": 19800,
"width": 25000,
"height": 20000,
"weight": 2.0,
"bei": 900,
"rightborder": 25000,
"stringsize": 150
},
{
"province": "江蘇",
"x": 11550,
"y": 21400,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 4000,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "江西",
"x": 11250,
"y": 21950,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 4000,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "河北",
"x": 11250,
"y": 20600,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 3000,
"rightborder": 25000,
"stringsize": 40
},
{
"province": "河南",
"x": 10900,
"y": 21200,
"width": 25000,
"height": 23000,
"weight": 1.0,
"bei": 2700,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "浙江",
"x": 11800,
"y": 21800,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 5000,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "湖北",
"x": 10800,
"y": 21500,
"width": 25000,
"height": 20000,
"weight": 1.0,
"bei": 3000,
"rightborder": 25000,
"stringsize": 30
},
{
"province": "湖南",
"x": 10800,
"y": 21900,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 3500,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "甘肅",
"x": 9000,
"y": 20300,
"width": 25000,
"height": 25000,
"weight": 2.0,
"bei": 1200,
"rightborder": 25000,
"stringsize": 70
},
{
"province": "福建",
"x": 11550,
"y": 22050,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 4000,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "西藏",
"x": 7500,
"y": 21000,
"width": 25000,
"height": 15000,
"weight": 1.5,
"bei": 1000,
"rightborder": 25000,
"stringsize": 100
},
{
"province": "貴州",
"x": 10350,
"y": 21900,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 3200,
"rightborder": 25000,
"stringsize": 35
},
{
"province": "遼寧",
"x": 11850,
"y": 20500,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 3300,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "重慶",
"x": 10500,
"y": 21700,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 4700,
"rightborder": 25000,
"stringsize": 20
},
{
"province": "陜西",
"x": 10450,
"y": 21000,
"width": 25000,
"height": 25000,
"weight": 1.0,
"bei": 2900,
"rightborder": 25000,
"stringsize": 25
},
{
"province": "青海",
"x": 8700,
"y": 20900,
"width": 25000,
"height": 15000,
"weight": 2.0,
"bei": 1500,
"rightborder": 25000,
"stringsize": 70
},
{
"province": "黑龍江",
"x": 12000,
"y": 19500,
"width": 25000,
"height": 18000,
"weight": 1.0,
"bei": 1500,
"rightborder": 25000,
"stringsize": 60
}
]
}
測試類
public static void main(String[] args) {
try {
ShpPictureController shpPictureController = new ShpPictureController();
// shpPictureController.doProvince();
shpPictureController.doNation();
} catch (Exception e) {
e.printStackTrace();
}
}
最終畫出的圖片效果如下:

到此這篇關(guān)于java使用GeoTools讀取shp文件并畫圖的操作代碼的文章就介紹到這了,更多相關(guān)java讀取shp文件并畫圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA設(shè)計模式零基礎(chǔ)解析之單例模式的八種方式
設(shè)計模式(Design pattern)是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過分類編目的、代碼設(shè)計經(jīng)驗的總結(jié)。使用設(shè)計模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性2021-10-10
spring?NamedContextFactory在Fegin配置及使用詳解
在我們?nèi)粘m椖恐?,使用FeignClient實現(xiàn)各系統(tǒng)接口調(diào)用變得更加簡單,?在各個系統(tǒng)集成過程中,難免會遇到某些系統(tǒng)的Client需要特殊的配置、返回讀取等需求。Feign使用NamedContextFactory來為每個Client模塊構(gòu)造單獨的上下文(ApplicationContext)2023-11-11
java selenium教程之selenium詳細(xì)介紹
本文主要介紹Java selenium,這里整理了selenium的一些基本資料,此軟件主要用于Web UI自動測試框架,有興趣的同學(xué)可以看一下2016-08-08
java根據(jù)不同的參數(shù)調(diào)用不同的實現(xiàn)類操作
這篇文章主要介紹了java根據(jù)不同的參數(shù)調(diào)用不同的實現(xiàn)類操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解
這篇文章主要介紹了MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
必須掌握的十個Lambda表達(dá)式簡化代碼提高生產(chǎn)力
這篇文章主要為大家介紹了必須掌握的十個Lambda表達(dá)式來簡化代碼提高生產(chǎn)力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

