You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何构建LineString类型GeoJSON?需创建哪些POJO生成对应格式?

Great questions! Let's tackle them one by one to get you up and running with GeoJSON and Java POJOs.

1. 如何制作LineString类型的GeoJSON格式?

LineString是GeoJSON里用于表示折线/线段的几何类型,它的结构遵循以下核心规则:

  • 作为独立Geometry对象时,type字段固定为"LineString"
  • coordinates字段是一个二维数组,每个子数组代表一个坐标点,格式为[经度, 纬度](注意是先经度后纬度,别搞反!)
  • 一条有效的LineString至少需要包含2个坐标点(毕竟一条线得有两个端点)

如果把它放到Feature(GeoJSON中用于封装几何的标准容器)里,完整示例如下:

{
  "type": "Feature",
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [-87.72158863628451, 41.88857748226596],
      [-87.71467926585602, 41.88753117344939]
    ]
  }
}

要是把它放进你目标中的FeatureCollection,只需要把这个Feature对象加入features数组即可。

2. 创建对应POJO生成目标GeoJSON结构

要生成包含LineString和Point要素的FeatureCollection,我们需要严格对应GeoJSON的层级结构设计POJO,同时保证序列化时能正确识别不同几何类型。下面是一套类型安全的POJO方案(用Jackson注解和Lombok简化代码,你也可以手动编写getter/setter替代Lombok):

核心POJO类

1. FeatureCollection(顶层容器)

对应GeoJSON的FeatureCollection,包含固定类型标识和Feature列表:

import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class FeatureCollection {
    // 固定值,无需外部设置
    @JsonProperty("type")
    private final String type = "FeatureCollection";
    
    @JsonProperty("features")
    private List<Feature> features;
}

2. Feature(要素容器)

每个Feature包含固定类型标识和对应的几何对象:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class Feature {
    @JsonProperty("type")
    private final String type = "Feature";
    
    @JsonProperty("geometry")
    private Geometry geometry;
}

3. Geometry(抽象父类)

作为所有几何类型的父类,用Jackson的类型注解自动识别子类对应的GeoJSON类型:

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data;

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
    // 映射LineString类型到对应的子类
    @JsonSubTypes.Type(value = LineStringGeometry.class, name = "LineString"),
    // 映射Point类型到对应的子类
    @JsonSubTypes.Type(value = PointGeometry.class, name = "Point")
})
public abstract class Geometry {
    private String type;
}

4. LineStringGeometry(LineString几何实现)

继承Geometry,实现LineString的坐标结构:

import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class LineStringGeometry extends Geometry {
    // 构造方法中设置固定类型
    public LineStringGeometry() {
        super.setType("LineString");
    }
    
    @JsonProperty("coordinates")
    private List<List<Double>> coordinates;
}

5. PointGeometry(Point几何实现)

继承Geometry,实现Point的坐标结构:

import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class PointGeometry extends Geometry {
    public PointGeometry() {
        super.setType("Point");
    }
    
    @JsonProperty("coordinates")
    private List<Double> coordinates;
}

使用示例

创建实例并序列化为JSON:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;

public class GeoJsonGenerator {
    public static void main(String[] args) throws Exception {
        // 创建LineString Feature
        LineStringGeometry lineGeom = new LineStringGeometry();
        List<List<Double>> lineCoords = new ArrayList<>();
        lineCoords.add(List.of(-87.72158863628451, 41.88857748226596));
        lineCoords.add(List.of(-87.71467926585602, 41.88753117344939));
        lineGeom.setCoordinates(lineCoords);
        Feature lineFeature = new Feature();
        lineFeature.setGeometry(lineGeom);
        
        // 创建Point Feature
        PointGeometry pointGeom = new PointGeometry();
        pointGeom.setCoordinates(List.of(-121.415061, 40.506229));
        Feature pointFeature = new Feature();
        pointFeature.setGeometry(pointGeom);
        
        // 组装FeatureCollection
        FeatureCollection collection = new FeatureCollection();
        collection.setFeatures(List.of(lineFeature, pointFeature));
        
        // 序列化为JSON
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(collection);
        System.out.println(json);
    }
}

运行这段代码就能生成你需要的GeoJSON结构啦。

内容的提问来源于stack exchange,提问作者Sebastian Cordova

火山引擎 最新活动