JSON文件数据格式如上,用底下的转换工具类即可完成转换,直接上代码
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zc.smartcity.ZtfServerApplicationTests;
import com.zc.smartcity.util.io.ReadHelper;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExchangeGeo {
ObjectMapper objMap = new ObjectMapper();
//记录转换总数
int i = 0;
//要读的文件名,文件放在当前包下的resource文件夹下
private String fileName = "tsjd.json";
//输出路径文件
private String outFileSrc = "E:/tsjd.json";
//geoNo 1: 转换高德 2: 转换百度
int geoNo = 1;
/**
* 读取Json 文件
*/
@Test
public void readJsonText() {
// 读取原始json文件并进行操作和输出
InputStream in = ReadHelper.getResourceAsStream(fileName);
FileWriter writer;
try {
String inStr = IOUtils.toString(in);
Map map = objMap.readValue(inStr, Map.class);
//获取features 节点集合
List<Map> featuresMap = (ArrayList) map.get("features");
for (int i = 0; i < featuresMap.size(); i++) {
//解析geometry节点下的coordinates点坐标
Map geometryMap = (HashMap) featuresMap.get(i).get("geometry");
List<ArrayList> coordinates = (ArrayList) geometryMap.get("coordinates");
for (int j = 0; j < coordinates.size(); j++) {
for (int k = 0; k < coordinates.get(j).size(); k++) {
List ll = (ArrayList) coordinates.get(j).get(k);
String geo = ll.get(0).toString() + "," +
ll.get(1).toString();
String[] exchange = getExchangeGeo(geo, geoNo);
ll.remove(0);
ll.remove(0);
ll.add(exchange[0]);
ll.add(exchange[1]);
}
}
//解析properties节点
if (featuresMap.get(i).get("properties") != null) {
Map propertiesMap = (HashMap) featuresMap.get(i).get("properties");
String CENTER_X = propertiesMap.get("CENTER_X").toString();
String CENTER_Y = propertiesMap.get("CENTER_Y").toString();
String geo2 = CENTER_X + "," + CENTER_Y;
String[] exchange = getExchangeGeo(geo2, geoNo);
propertiesMap.put("CENTER_X", exchange[0]);
propertiesMap.put("CENTER_Y", exchange[1]);
}
}
String write = objMap.writeValueAsString(map);
writer = new FileWriter(outFileSrc);
writer.write(write);
writer.flush();
writer.close();
System.out.println(write);
} catch (IOException e) {
e.printStackTrace();
}
}
public String[] getExchangeGeo(String geo, int getNo) {
String gaode = "http://restapi.amap.com/v3/assistant/coordinate/convert?coordsys=gps&output=json&key=1aed248ea252089beb3c5849a200729f&locations=" +
geo;
String baidu =
"http://api.map.baidu.com/geoconv/v1/?from=1&to=5&ak=ksED9LPja0IdvTA3n7WK2p8cyDCfg6uk&coords="
+ geo;
/** 百度转换参数说明:
*from 源坐标类型:
1:GPS设备获取的角度坐标,wgs84坐标;
2:GPS获取的米制坐标、sogou地图所用坐标;
3:google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标,国测局(gcj02)坐标;
4:3中列表地图坐标对应的米制坐标;
5:百度地图采用的经纬度坐标;
6:百度地图采用的米制坐标;
7:mapbar地图坐标;
8:51地图坐标
int 1 1 否
to
目标坐标类型:
3:国测局(gcj02)坐标;
4:3中对应的米制坐标;
5:bd09ll(百度经纬度坐标);
6:bd09mc(百度米制经纬度坐标) int 5 5 否
*/
String GET_URL;
if (getNo == 1) {
GET_URL = gaode;
} else {
GET_URL = baidu;
}
String outStr = null;
StringBuilder sb = new StringBuilder();
Boolean flag = true;
while (flag) {
try {
URL url = new URL(GET_URL); // 把字符串转换为URL请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
System.out.println("num:" + i + sb.toString());
i++;
flag = false;
break;
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
}
//String 转Map
try {
Map map = objMap.readValue(sb.toString(), Map.class);
if (getNo == 1) {
outStr = map.get("locations").toString();
}
if (getNo == 2) {
List list = (ArrayList) map.get("result");
Map geoMap = new HashMap();
for (int j = 0; j < list.size(); j++) {
geoMap = (HashMap) list.get(j);
}
outStr = geoMap.get("x").toString() + "," + geoMap.get("y").toString();
}
} catch (IOException e) {
e.printStackTrace();
}
String[] outStrArr = outStr.split(",");
return outStrArr;
}
}
下面为上面读取文件变成流用到的方法
/**
*
* 读取CLASSPATH路径的文件
*
* @param strResource
* CLASSPATH路径
* @return 文件流
*/
public static InputStream getResourceAsStream(String strResource) {
InputStream is = null;
URL url = Thread.currentThread().getContextClassLoader().getResource(strResource);
if (null != url) {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(strResource);
} else {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream("../" + strResource);
}
if (null == is) {
is = ClassLoader.getSystemResourceAsStream(strResource);
}
if (is == null) {
is = Environment.class.getResourceAsStream(strResource);
}
if (is == null) {
is = Environment.class.getClassLoader().getResourceAsStream(strResource);
}
if (is == null) {
try {
is = new FileInputStream(strResource);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return is;
}