项目中需要通过一条线路,计算一个在辐射范围内包含该线路的图形。使用JTS包计算缓冲区即可得到。
1.引入依赖
<!-- jts依赖-->
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts-core</artifactId>
<version>1.14.0</version>
</dependency>
<!--解析字符串-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.1</version>
</dependency>
2.调用buffer方法获得结果
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import java.util.List;
/**
* 获取缓冲区
*/
public class Mytest {
public static void main(String[] args) {
String position = "[113.2136306762695,23.2724609375],[113.2136001586914,23.27313613891602],[113.2136001586914,23.27411651611328],[113.2136154174805,23.27562141418457],[113.2136154174805,23.27562141418457],[113.2136459350586,23.27606582641602],[113.2136459350586,23.27606582641602],[113.2151107788086,23.27617835998535],[113.2151107788086,23.27617835998535],[113.2170639038086,23.27627754211426],[113.2213287353516,23.27650451660156],[113.2219467163086,23.27655601501465]";
JSONObject jsonObject = JSONUtil.parseObj("{\"type\":\"LineString\",\"coordinates\":["+ position +"]}");
JSONArray jsonArray = jsonObject.getJSONArray("coordinates");
Coordinate coorArr[] = new Coordinate[jsonArray.size()];
//封装数据
for (int i = 0; i < jsonArray.size(); i++){
String pointArr[] = jsonArray.get(i).toString().replace("[", "").replace("]", "").split(",");
if (pointArr != null && pointArr.length == 2){
Coordinate coord = new Coordinate(Double.parseDouble(pointArr[0]),Double.parseDouble(pointArr[1]));
coorArr[i]=coord;
}
}
GeometryFactory geoFac = new GeometryFactory();
LineString createLineString = geoFac.createLineString(coorArr);
// 距离转换角度 = meter / (2*Math.PI*6371004)*360
//线路附近100米范围
Geometry buffer = createLineString.buffer(100 / (2*Math.PI*6371004)*360);
//将结果封装返回
String subBetween = StrUtil.subBetween(buffer.toString(),"((", "))");
List<String> split = StrUtil.split(subBetween, ", ");
String result = "";
for (String item: split) {
List<String> split1 = StrUtil.split(item, " ");
String temp = "["+ split1.get(0) +"," + split1.get(1) +"]" +",";
result = result + temp;
}
System.out.println(result);
}
}
3.效果
4.参考资料
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦