通过坐标点生成geojson格式的Polygon文件(Java)
在实际项目中,geojson格式的文件应用场景非常多,比如:shapefile转geojson,加载geojson数据到openlayers、leaflet等开源地图组件中 等,还有些情况下需要自己把一系列点生成面,然后把面再转成geojson,之后把生成的geojson格式文件提供给前端使用,这里以java为例,实现坐标点生成geojson格式的Polygon文件。首先引入geotools相关j
·
在实际项目中,geojson格式的文件应用场景非常多,比如:shapefile转geojson,加载geojson数据到openlayers、leaflet等开源地图组件中 等,还有些情况下需要自己把一系列点生成面,然后把面再转成geojson,之后把生成的geojson格式文件提供给前端使用,这里以java为例,实现坐标点生成geojson格式的Polygon文件。
首先引入geotools相关jar包
<!-- geojson -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>23.2</version>
</dependency>
<!-- 项目的pom文件中要引入包含geotools相关jar的仓库地址,不然geotools的jar包可能会拉取不下来 -->
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>osgeo-release</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>https://repo.boundlessgeo.com/main/</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
新建一个点数据转geojson格式的ploygon的类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
public class File2Geojson {
public static void main(String[] args) throws IOException {
//读取坐标点文件
//文件格式如下:104.048574,30.752506;104.050062,30.751254;104.050801,30.750952|104.08962,30.670089;104.08684,30.665675;104.086489,30.665157|104.059962,30.647005;104.06156,30.649142;104.062694,30.650745;104.063921,30.652742
//格式说明:符号 | 用来分割不同的Polygon
// 符号 ; 分割不同的坐标点
// 符号 , 分割经纬度,前面为经度,后面为纬度
File file = new File("C:\\Users\\Administrator\\Desktop\\log\\zuobiao.txt");
FileInputStream inputStream = new FileInputStream(file);
int length = inputStream.available();
byte bytes[] = new byte[length];
inputStream.read(bytes);
inputStream.close();
String str = new String(bytes, StandardCharsets.UTF_8);
String[] polygonPointArr = str.split("\\|");
//创建Polygon
Polygon[] polygons=new Polygon[polygonPointArr.length];
for (int i = 0; i < polygonPointArr.length; i++) {
System.out.println("第" + (i + 1) + "个");
String[] pointArr = polygonPointArr[i].split(";");
List<Coordinate> polygonCoordinateList = new ArrayList<>();
for (int j = 0; j < pointArr.length; j++) {
String pointStr = pointArr[j];
String[] point = pointStr.split(",");
polygonCoordinateList.add(new Coordinate(Double.parseDouble(point[0]), Double.parseDouble(point[1])));
}
polygons[i] = new GeometryFactory().createPolygon(polygonCoordinateList.toArray(new Coordinate[0]));
}
//将Polygon数组转换成MultiPolygon
MultiPolygon multiPolygon = new GeometryFactory().createMultiPolygon(polygons);
//创建要素类
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("geometry");
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.add("the_geom", multiPolygon.getClass());
final SimpleFeatureType TYPE = builder.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
featureBuilder.add(multiPolygon);
SimpleFeature feature = featureBuilder.buildFeature(null);
//将要素类转换成geojson
FeatureJSON fjson = new FeatureJSON();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String geojson = writer.toString();
//输出geojson字符串到文件中
FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\log\\chengdu.json");
fos.write(geojson.getBytes());
fos.close();
System.out.println("完成");
}
}
我们打开可以查看geojson格式文件的工具:
http://tool.tooleek.com/tools/mapshaper
选择生成好的geojson格式的文件,打开就可以看到下面的效果
geojson格式文件成功生成,并能提供给前端使用,前端也可以显示到地图上
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)