编写程序判断附录中的四个点:p1、p2、p3、p4,是否在同级的经纬度⽂件latlon.json框起来的范 围内
时间: 2023-08-30 08:07:02 浏览: 359
根据描述,我们可以将问题分解为以下几个步骤:
1. 读取经纬度文件latlon.json,获取框起来的范围。
2. 定义四个点的经纬度坐标。
3. 判断四个点是否在范围内。
下面是一个Java示例程序,使用了json、gson和GeoTools库来实现:
```java
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class CheckPoints {
public static void main(String[] args) throws IOException, FactoryException {
// 读取经纬度文件,获取框起来的范围
FileReader reader = new FileReader("latlon.json");
List<List<Double>> bounds = new Gson().fromJson(reader, new TypeToken<List<List<Double>>>() {}.getType());
reader.close();
// 定义四个点的经纬度坐标
Coordinate p1 = new Coordinate(116.327, 39.979);
Coordinate p2 = new Coordinate(116.327, 39.980);
Coordinate p3 = new Coordinate(116.329, 39.980);
Coordinate p4 = new Coordinate(116.329, 39.979);
// 创建多边形的JTS几何对象
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
Coordinate[] coords = new Coordinate[bounds.size()];
for (int i = 0; i < bounds.size(); i++) {
List<Double> bound = bounds.get(i);
coords[i] = new Coordinate(bound.get(0), bound.get(1));
}
Polygon polygon = geometryFactory.createPolygon(coords);
// 创建点的JTS几何对象
Point point1 = geometryFactory.createPoint(p1);
Point point2 = geometryFactory.createPoint(p2);
Point point3 = geometryFactory.createPoint(p3);
Point point4 = geometryFactory.createPoint(p4);
// 判断四个点是否在范围内
boolean inPolygon1 = point1.within(polygon);
boolean inPolygon2 = point2.within(polygon);
boolean inPolygon3 = point3.within(polygon);
boolean inPolygon4 = point4.within(polygon);
System.out.println("p1 in polygon: " + inPolygon1);
System.out.println("p2 in polygon: " + inPolygon2);
System.out.println("p3 in polygon: " + inPolygon3);
System.out.println("p4 in polygon: " + inPolygon4);
}
}
```
注意事项:
- 经纬度文件latlon.json中的范围应该是一个多边形,且顶点按照顺时针或逆时针方向排列。
- 由于GeoTools的性能较低,处理大量数据时可能会较慢。如果需要高性能的GIS库,可以考虑使用JTS或Spatial4j等。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)