直入正题,说一下大概思路
1.将shp转为geojson格式
2.将geojson里面的坐标串进行坐标转换
3.将转换好的新的geojson重新生成shp格式
下面我们具体说明操作步骤
1.利用https://mapshaper.org/工具,将shp转geojson
点击Export,导出geojson
2.利用JAVA+fastjson对geojson进行修改和编辑
主要代码如下:
package com.company;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
public class Main {
public static 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;
}
}
public static void readJson(String jsonPath) throws IOException {
String s = readJsonFile(jsonPath);
//GeometryCollection geometryCollection = JSONObject.parseObject(s, GeometryCollection.class);
JSONObject jobj = JSON.parseObject(s);
JSONArray arr = jobj.getJSONArray("features");//构建JSONArray数组
ArrayList<JSONObject> geoList=new ArrayList<JSONObject>();
ArrayList<JSONObject> tobj=new ArrayList<JSONObject>();
for(int i=0;i<arr.size();i++){
JSONObject JO = (JSONObject)arr.get(i);
JSONObject ar=(JSONObject)JO.get("geometry");
if(ar==null)
{
continue;
}
String typ=(String)ar.get("type");
if(typ.equals("MultiPolygon")){
continue;
}
JSONArray nar1=(JSONArray)ar.get("coordinates");
JSONArray nar=(JSONArray)nar1.get(0);
JSONObject pro=(JSONObject)JO.get("properties");
//写json
JSONObject polyobj = new JSONObject();
//polyobj.put("coordinates",Polygon.class);
String strSum="";
ArrayList<double[]> corlist=new ArrayList<double[]>();
ArrayList<ArrayList> t=new ArrayList<ArrayList>();
if(i==2727){
System.out.print(i+"xiao");
}
for(int j=0;j<nar.size();j++){
JSONArray dt=(JSONArray)nar.get(j);
System.out.print(i+"xiao"+j+'\t');
//JSONArray xyArr=(JSONArray)dt.get(0);
BigDecimal x1=(BigDecimal)dt.get(0);
double x=x1.doubleValue();
BigDecimal y2=(BigDecimal)dt.get(1);
double y=y2.doubleValue();
double[] dtar=WGS_Encrypt.WGS2Mars(y,x);
corlist.add(dtar);
}
t.add(corlist);
JSONObject corobj = new JSONObject();
corobj.put("type","Polygon");
corobj.put("coordinates",t);
//geoList.add(corobj);
JSONObject corobj2 = new JSONObject();
corobj2.put("type","Feature");
corobj2.put("geometry",corobj);
corobj2.put("properties",pro);
tobj.add(corobj2);
//JSONArray ar=(JSONArray) arr.get(i);
}
//写JSON
JSONObject wrobj = new JSONObject();
wrobj.put("type","FeatureCollection");
wrobj.put("features",tobj);
String result=wrobj.toString();
FileWriter fw = new FileWriter("D:\\temp\\outhfnew.json");
PrintWriter out = new PrintWriter(fw);
out.write(result);
out.println();
fw.close();
out.close();
}
public static void main(String[] args) throws Exception {
readJson("F:\\BaiduNetdiskDownload\\hfnew.json");
}
}
3.形成新的geojson后,再通过https://mapshaper.org/工具,转成shp
4.用cesiumlab工具将shp转成3dtiles
5.加载数据,我们可以看到建筑面数据已经成功转换完成。