geojson序列化
2025-07-20 07:30:37 # gis

由于项目需要使用到postgis中的空间数据, 查询返回的数据,无论是wkt还是geojson都是返回的字符串格式, 开始使用字符串返回前端,前端在序列化,这操作不优雅, 后边使用map兼容所有格式,但是发现返回的数据 数字太长了, 转为了字符串, 前端拿到的就是字符串格式的数据,还是不优雅,于是想自己写一个geojson的对象封装, 各种查询博客以后,发现ObjectMapper可以按照字段对象类型去序列化, 也是spring官方推荐,比hutool好了不止一点

自定义对象按照geojson对象定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeoJsonFeature implements Serializable {

/**
* 类型
*/
private String type="FeatureCollection";
/***
* features
*/
private List<GeoFeature> features;

/***
* 参考系
*/
private GeoCrs crs;

/**
* 图层名称
*/
private String layerName;

/**
* 总条数
*/
private long total=0;
// 其他代码 getter and setter
}
1
2
3
4
5
6
7
8
9
10
11
12
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GeoFeature {
private String type;
private Map<String,Object> properties=new HashMap<>();

private GeoGeometry geometry;

private List<GeoGeometry> geometries;// collecton集合序列化列表
}

核心类实现,通过不同的类型实现到不同的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@NoArgsConstructor
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,property = "type")
@JsonSubTypes(value = {
@JsonSubTypes.Type(value = GeoPoint.class, name = "Point"),
@JsonSubTypes.Type(value = GeoPoint.class, name = "PointZ"),
@JsonSubTypes.Type(value = GeoLineString.class, name = "LineString"),
@JsonSubTypes.Type(value = GeoLineString.class, name = "LineStringZ"),
@JsonSubTypes.Type(value = GeoPolygon.class, name = "Polygon"),
@JsonSubTypes.Type(value = GeoPolygon.class, name = "PolygonZ"),
@JsonSubTypes.Type(value = GeoMultiPoint.class, name = "MultiPoint"),
@JsonSubTypes.Type(value = GeoMultiPoint.class, name = "MultiPointZ"),
@JsonSubTypes.Type(value = GeoMultiLineString.class, name = "MultiLineString"),
@JsonSubTypes.Type(value = GeoMultiLineString.class, name = "MultiLineStringZ"),
@JsonSubTypes.Type(value = GeoMultiPolygon.class, name = "MultiPolygon"),
@JsonSubTypes.Type(value = GeoMultiPolygon.class, name = "MultiPolygonZ"),
@JsonSubTypes.Type(value = GeoGeometryCollection.class, name = "GeometryCollection"),
@JsonSubTypes.Type(value = GeoGeometryCollection.class, name = "GeometryCollectionZ"),
})
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public abstract class GeoGeometry {

private String type;

public String toString(){ // 重写图string,方便调试,直接序列化为json结构
return JacksonUtil.toJsonString(this);
}
}

piont:

1
2
3
4
5
6
7
8
9
10
11
@NoArgsConstructor
@AllArgsConstructor
@Data
public class GeoPoint extends GeoGeometry {
private List<Double> coordinates;
@Override
public String toString() {
return super.toString();
}
}

line

1
2
3
4
5
6
@NoArgsConstructor
@AllArgsConstructor
@Data
public class GeoLineString extends GeoGeometry {
private List<List<Double>> coordinates;
}

polygon

1
2
3
4
5
6
7
8
@NoArgsConstructor
@AllArgsConstructor
@Data
public class GeoPolygon extends GeoGeometry {

private List<List<List<Double>>> coordinates;
}

其余子类类似,主要定义不同类型的数组结构,