在實際項目中,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格式檔案成功生成,并能提供給前端使用,前端也可以顯示到地圖上