è®°å½ä½¿ç¨geotoolså·¥å ·ï¼å®ç°shpågeojsonæ°æ®äºè½¬
ç¬åï¼ä¸ä½¿ç¨ä¾èµï¼vividsolutions ï¼å 为 1.8 ä¸ geotools 20以åçæ¬jts ä¸ä¸è´ï¼ä¼æ¥éã
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.8</version>
<type>pom</type>
</dependency>
1. å¼èµ·pomä¾èµjar
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geometry</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-jts-wrapper</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
2. ä¸ä»£ç ï¼èªå¨è¯å«åæ ç³»ï¼é»è®¤ä¸º84åæ
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.*;
import org.geotools.data.FeatureWriter;
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.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
/**
* @author qiaobing1226
* @since 2022/10/28 ä¸å10:52
* åèï¼https://blog.csdn.net/qiaobing1226/article/details/127612665
*/
public class Shp2GeojsonUtils {
private static final String POINT = "Point";
private static final String MULTIPOINT = "MultiPoint";
private static final String LINESTRING = "LineString";
private static final String MULTILINESTRING = "MultiLineString";
private static final String POLYGON = "Polygon";
private static final String MULTIPOLYGON = "MultiPolygon";
private static final String THE_GEOM = "the_geom";
private static final String PROPERTIES = "properties";
private static final String GEOMETRY = "geometry";
private static final String GBK = "GBK";
/**
* geoJson转æ¢ä¸ºshpæä»¶
*
* @param jsonPath
* @param shpPath
* @return
*/
public static Map<String, Object> geoJson2Shape(String jsonPath, String shpPath) {
Map<String, Object> map = new HashMap<>();
GeometryJSON geoJson = new GeometryJSON();
try {
JSONObject json = readGeoJsonFile(jsonPath);
JSONArray features = (JSONArray) json.get("features");
JSONObject feature0 = JSONObject.parseObject(features.get(0).toString());
// è·å屿§åç§°
Set properties = JSONObject.parseObject(feature0.getString(PROPERTIES)).keySet();
String strType = ((JSONObject) feature0.get(GEOMETRY)).getString("type");
String strCrs = json.getJSONObject("crs").getJSONObject(PROPERTIES).getString("name");
CoordinateReferenceSystem crs = CRS.decode(strCrs);
ShapefileDataStore shapefileDataStore = dataStore(properties, strType, shpPath, crs);
if (shapefileDataStore == null) {
return map;
}
// 设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = shapefileDataStore.getFeatureWriter(shapefileDataStore.getTypeNames()[0],
Transaction.AUTO_COMMIT);
for (int i = 0, len = features.size(); i < len; i++) {
String strFeature = features.get(i).toString();
Reader reader = new StringReader(strFeature);
SimpleFeature feature = writer.next();
switch (strType) {
case POINT:
feature.setAttribute(THE_GEOM, geoJson.readPoint(reader));
break;
case MULTIPOINT:
feature.setAttribute(THE_GEOM, geoJson.readMultiPoint(reader));
break;
case LINESTRING:
feature.setAttribute(THE_GEOM, geoJson.readLine(reader));
break;
case MULTILINESTRING:
feature.setAttribute(THE_GEOM, geoJson.readMultiLine(reader));
break;
case POLYGON:
feature.setAttribute(THE_GEOM, geoJson.readPolygon(reader));
break;
case MULTIPOLYGON:
feature.setAttribute(THE_GEOM, geoJson.readMultiPolygon(reader));
break;
}
Iterator iterator = properties.iterator();
while (iterator.hasNext()) {
String str = iterator.next().toString();
JSONObject element = JSONObject.parseObject(features.get(i).toString());
feature.setAttribute(str, JSONObject.parseObject(element.getString(PROPERTIES)).get(str));
}
writer.write();
}
writer.close();
shapefileDataStore.dispose();
map.put("status", 200);
map.put("message", "shp转æ¢success");
} catch (Exception e) {
map.put("status", 400);
map.put("message", e.getMessage());
e.printStackTrace();
}
return map;
}
/**
* 读ågeojosnæä»¶
*
* @param jsonPath
* @return
*/
private static JSONObject readGeoJsonFile(String jsonPath) {
// 读æä»¶å°Stringbuffer
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(jsonPath));
String str;
while ((str = br.readLine()) != null) {// éè¡è¯»å
sb.append(str + "\r\n");
}
br.close();
} catch (Exception e) {
if (br != null) {
try {
br.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
e.printStackTrace();
}
return JSONObject.parseObject(sb.toString());
}
/**
* 设置shpæä»¶å±æ§
*
* @param properties
* @param strType
* @param shpPath
* @return
*/
private static ShapefileDataStore dataStore(Set properties, String strType, String shpPath, CoordinateReferenceSystem crs) {
try {
Class<?> geoType = null;
switch (strType) {
case POINT:
geoType = Point.class;
break;
case MULTIPOINT:
geoType = MultiPoint.class;
break;
case LINESTRING:
geoType = LineString.class;
break;
case MULTILINESTRING:
geoType = MultiLineString.class;
break;
case POLYGON:
geoType = Polygon.class;
break;
case MULTIPOLYGON:
geoType = MultiPolygon.class;
break;
}
// å建shapeæä»¶å¯¹è±¡
File file = new File(shpPath);
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();
//é»è®¤84åæ
tb.setCRS(crs == null ? DefaultGeographicCRS.WGS84 : crs);
tb.setName("shapefile");
// ç±»åï¼Point/MultiPoint/LineString/MultiLineString/Polygon/MultiPolygon
tb.add("the_geom", geoType);
Iterator propertiesIter = properties.iterator();
// è®¾ç½®å±æ§
while (propertiesIter.hasNext()) {
String str = propertiesIter.next().toString();
// æ¤å¤è®¾ç½®ä¸ºstring
tb.add(str, String.class);
}
ds.createSchema(tb.buildFeatureType());
// 设置ç¼ç
Charset charset = Charset.forName(GBK);
ds.setCharset(charset);
return ds;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* shpæä»¶è½¬æ¢geojsonæ°æ®
*
* @param shpPath
* @return
*/
public static Map<String, Object> shp2Geojson(String shpPath, String jsonPath) {
Map<String, Object> map = new HashMap();
//æ°å»ºjson对象
JSONObject geojsonObject = new JSONObject();
geojsonObject.put("type", "FeatureCollection");
try {
JSONArray array = new JSONArray();
String fileName = readShpContent(shpPath, array);
geojsonObject.put("features", array);
geojsonObject.put("name", fileName);
String crs = getCoordinateSystemWKT(shpPath);
//GEOGCS表示è¿ä¸ªæ¯å°ååæ ç³»,PROJCSå表示æ¯å¹³é¢æå½±åæ ç³»
JSONObject crsJson = new JSONObject();
JSONObject proJson = new JSONObject();
crsJson.put("type", "name");
if (crs.startsWith("PROJCS")) {
proJson.put("name", "urn:ogc:def:crs:EPSG::3857");
crsJson.put("properties", proJson);
} else {
proJson.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
crsJson.put("properties", proJson);
}
geojsonObject.put("crs", crsJson);
// itertor.close();
long startTime = System.currentTimeMillis();
//å°jsonå符串使ç¨å符æµåå ¥æä»¶
/* File outputfile=new File(jsonPath);
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(outputfile));
bufferedWriter.write(JSON.toJSONString(geojsonObject));
bufferedWriter.flush();
bufferedWriter.close();*/
File outputfile = new File(jsonPath);
FileOutputStream fileOutputStream = new FileOutputStream(outputfile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
outputStreamWriter.write(JSON.toJSONString(geojsonObject));
outputStreamWriter.flush();
outputStreamWriter.close();
//å°jsonå符串使ç¨åèæµåå ¥æä»¶
/* File outputfile=new File(jsonPath);
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(outputfile));
byte[] bytes= JSON.toJSONString(geojsonObject).getBytes("utf-8");
bufferedOutputStream.write(bytes);
//fileOutputStream.write(JSON.toJSONString(geojsonObject));
bufferedOutputStream.flush();
bufferedOutputStream.close();*/
// long endTime=System.currentTimeMillis();
// System.out.println("å½åç¨åºèæ¶ï¼"+(endTime-startTime)+"ms");
} catch (Exception e) {
map.put("status", "failure");
map.put("message", e.getMessage());
e.printStackTrace();
}
//
return geojsonObject;
}
private static String readShpContent(String shpPath, JSONArray array) {
String fileName = "";
try {
FeatureJSON fjson = new FeatureJSON();
//è·åfeaturecollection
File file = new File(shpPath);
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(file.toURL());
//设置ç¼ç
/* Charset charset = Charset.forName("GBK");
shpDataStore.setCharset(charset);*/
fileName = shpDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = null;
featureSource = shpDataStore.getFeatureSource(fileName);
SimpleFeatureCollection result = featureSource.getFeatures();
SimpleFeatureIterator itertor = result.features();
//éåfeature转为json对象
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String temp = writer.toString();
Object geometry = JSONObject.parseObject(temp).getString(GEOMETRY);
byte[] b = temp.getBytes("iso8859-1");
temp = new String(b, GBK);
JSONObject json = JSON.parseObject(temp);
array.add(json);
}
itertor.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
/**
* è·åShapeæä»¶çåæ ç³»ä¿¡æ¯,GEOGCS表示è¿ä¸ªæ¯å°ååæ ç³»,PROJCSå表示æ¯å¹³é¢æå½±åæ ç³»
*
* @shpPath
*/
public static String getCoordinateSystemWKT(String shpPath) {
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = null;
try {
dataStore = (ShapefileDataStore) factory.createDataStore(new File(shpPath).toURI().toURL());
return dataStore.getSchema().getCoordinateReferenceSystem().toWKT();
} catch (UnsupportedOperationException | IOException e) {
e.printStackTrace();
} finally {
dataStore.dispose();
}
return "";
}
/**
* å·¥å ·ç±»æµè¯æ¹æ³
*
* @param args
*/
public static void main(String[] args) throws Exception {
Shp2GeojsonUtils shpToGeojson = new Shp2GeojsonUtils();
// // shape2Geojson
String shpPath = "/Users/ecarx/Desktop/geojson/ä¸ä¸é«æ¶mct/123/AD_Road.shp";
String jsonPath = "/Users/ecarx/Desktop/geojson/AD_Road.geojson";
Map<String, Object> map = shpToGeojson.shp2Geojson(shpPath, jsonPath);
// geojson2Shape
// String shpPath = "/Users/ecarx/Desktop/geojson/ä¸ä¸é«æ¶mct/123/AD_Road.shp";
// String jsonPath = "/Users/ecarx/Desktop/geojson/ä¸ä¸é«æ¶mct/AD_Road.geojson";
// Map<String, Object> map = shpToGeojson.geoJson2Shape(jsonPath, shpPath);
// System.out.println(map.toString());
}
}
ââââââââââââââââ
çæå£°æï¼æ¬æä¸ºCSDNå主ãqiaobing1226ãçååæç« ï¼éµå¾ªCC 4.0 BY-SAçæåè®®ï¼è½¬è½½è¯·éä¸åæåºå¤é¾æ¥åæ¬å£°æã
åæé¾æ¥ï¼https://blog.csdn.net/qiaobing1226/article/details/127612665