package cn.ssk.geotools;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.GeoTools;
import org.opengis.feature.simple.SimpleFeature;
import cn.ssk.mysql.AreaInfo;
import cn.ssk.mysql.MysqlUtils;
import sun.nio.cs.ext.GBK;
public class GeotoolDemo {
private static HashMap codeMap=MysqlUtils.getCodeMap();
private static HashMap jsonFiles=new HashMap<>();
public static void main(String[] args)throws Exception {
URL url=GeoTools.class.getClassLoader().getResource("xian.shp");
File file=new File(url.toURI());
handleProcess(file);
}
public static void handleProcess(File shpfile)throws Exception{
ShapefileDataStore fdstore=(ShapefileDataStore)FileDataStoreFinder.getDataStore(shpfile);
fdstore.setCharset(new GBK());
SimpleFeatureSource source=fdstore.getFeatureSource();
SimpleFeatureCollection sfc=source.getFeatures();
try(SimpleFeatureIterator iterator=sfc.features()){
AreaInfo info;
while(iterator.hasNext()){
SimpleFeature feature=iterator.next();
String cityName=feature.getAttribute("NAME").toString();
String polygon=feature.getAttribute("the_geom").toString();
info=codeMap.get(cityName);
if(info!=null){
Integer mgr=info.getMgr();
String coordinates=parseCoordinate(polygon);
if(jsonFiles.containsKey(mgr.toString())){
FileWriter fw=jsonFiles.get(mgr.toString());
fw.append(",");
appendToFile(fw,coordinates,cityName);
}else{
FileWriter fw=new FileWriter(mgr.toString()+".json",true);
fw.append("{\"type\":\"FeatureCollection\",\"features\":[");
fw.flush();
jsonFiles.put(mgr.toString(), fw);
appendToFile(fw, coordinates,cityName);
}
}
}
for(String filename :jsonFiles.keySet()){
FileWriter writer=jsonFiles.get(filename);
writer.append("]}");
writer.flush();
writer.close();
}
}
}
public static void appendToFile(FileWriter writer,String coordinate,String cityName) throws IOException{
writer.append("{\"type\":\"Feature\",\"properties\":{\"name\":\"");
writer.append(cityName);
writer.append("\"},");
writer.append("\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":");
writer.append(coordinate);
writer.append("}}");
writer.flush();
}
private static String geojsonWrapper(String polygon){
StringBuilder sber=new StringBuilder(polygon.length()+30);
String tmp[]=polygon.split(",");
sber.append("[[[");
for(int i=0;i
sber.append("[");
sber.append(tmp[i].trim().replace(" ", ","));
sber.append("]");
if(i != tmp.length-1)
sber.append(",");
}
sber.append("]]]");
return sber.toString();
}
public static String parseCoordinate(String polygon){
int beginIndex=polygon.indexOf("(((")+3;
int endIndex=polygon.lastIndexOf(")))");
String s=polygon.substring(beginIndex,endIndex).replace('(', ' ').replace(')', ' ');
return geojsonWrapper(s);
}
}
根据县的名称,查询县所属的市,该关系存入一个数据库表中,也可存入文件中。
package cn.ssk.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
public class MysqlUtils {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/geotools";
private static final String username="root";
private static final String password="123456";
private static Connection conn=null;
private static PreparedStatement stmt=null;
private static ResultSet rs=null;
private static HashMap codeMap=new HashMap<>();
static{
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, username, password);
init();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs,PreparedStatement stmt,Connection conn){
try {
if(rs!=null){rs.close();}
if(stmt!=null){stmt.close();}
if(conn!=null){conn.close();}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void init(){
AreaInfo info; //数据库表对应bean
try {
stmt=conn.prepareStatement("select code,cityName,mgr from areacode where code like ?");
stmt.setString(1, "15____");//根据省份编号仅获取该省县市的编码对应关系
rs=stmt.executeQuery();
while(rs.next()){
info=new AreaInfo();
info.setCode(rs.getInt("code"));
info.setCityName(rs.getString("cityName"));
info.setMgr(rs.getInt("mgr"));
codeMap.put(rs.getString("cityName"), info);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
close(rs,stmt,conn);
}
}
public static HashMap getCodeMap() {
return codeMap;
}
}
数据库行政区划代码关系表(数据来自国家统计局最新数据):
code:县或市的行政区划代码,mgr 县所属的市的代码