Skip to content

Commit

Permalink
v1.6 FeatureCollection增加header属性
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Feb 27, 2023
1 parent c8ab176 commit 159f345
Show file tree
Hide file tree
Showing 14 changed files with 2,844 additions and 2,373 deletions.
4 changes: 2 additions & 2 deletions giscat-vector/giscat-vector-mbexpression/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>giscat-vector</artifactId>
<groupId>org.wowtools</groupId>
<version>g1.5.2</version>
<version>g1.6.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>giscat-vector-mbexpression</artifactId>
<version>g1.5.2</version>
<version>g1.6.0</version>
<description>mapbox expressions表达式解析为java对象,用以支持数据过滤等场景</description>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions giscat-vector/giscat-vector-mvt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>giscat</artifactId>
<groupId>org.wowtools</groupId>
<version>g1.5.2</version>
<version>g1.6.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>giscat-vector-mvt</artifactId>
<version>g1.5.2</version>
<version>g1.6.0</version>
<description>Mapbox vector tile (mvt) 的序列化与反序列化</description>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions giscat-vector/giscat-vector-pojo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>giscat-vector</artifactId>
<groupId>org.wowtools</groupId>
<version>g1.5.2</version>
<version>g1.6.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>giscat-vector-pojo</artifactId>
<version>g1.5.2</version>
<version>g1.6.0</version>
<dependencies>
<dependency>
<groupId>org.locationtech.jts</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.wowtools.giscat.vector.pojo;

import java.util.List;
import java.util.Map;

/**
* FeatureCollection
Expand All @@ -28,13 +29,29 @@
* @date 2022/3/15
*/
public class FeatureCollection {
/**
* 要素
*/
private List<Feature> features;

/**
* 头信息,可以在headers中添加一些关于FeatureCollection、features等的描述信息
*/
private Map<String, Object> headers;

public List<Feature> getFeatures() {
return features;
}

public void setFeatures(List<Feature> features) {
this.features = features;
}

public Map<String, Object> getHeaders() {
return headers;
}

public void setHeaders(Map<String, Object> headers) {
this.headers = headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public String toGeoJsonString() {
public static final class FeatureCollection {
private final String type = "FeatureCollection";
private Feature[] features;
private Map<String, Object> headers;

@JsonIgnore
public String toGeoJsonString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
****************************************************************/
package org.wowtools.giscat.vector.pojo.converter;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -45,6 +46,9 @@ public class GeoJsonFeatureConverter {
* jackson ObjectMapper
*/
public static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

private static final JavaType typeGeometry = mapper.constructType(GeoJsonObject.Geometry.class);
private static final JavaType typeFeature = mapper.constructType(GeoJsonObject.Feature.class);
Expand Down Expand Up @@ -102,14 +106,20 @@ public static GeoJsonObject.Feature toGeoJson(Feature feature) {
* @return geojson
*/
public static GeoJsonObject.FeatureCollection toGeoJson(FeatureCollection featureCollection) {
GeoJsonObject.FeatureCollection geoJsonFeatureCollection = new GeoJsonObject.FeatureCollection();

if (null != featureCollection.getHeaders()) {
geoJsonFeatureCollection.setHeaders(featureCollection.getHeaders());
}

GeoJsonObject.Feature[] geoJsonFeatures = new GeoJsonObject.Feature[featureCollection.getFeatures().size()];
int i = 0;
GeoJsonObject.FeatureCollection geoJsonFeatureCollection = new GeoJsonObject.FeatureCollection();
for (Feature feature : featureCollection.getFeatures()) {
geoJsonFeatures[i] = toGeoJson(feature);
i++;
}
geoJsonFeatureCollection.setFeatures(geoJsonFeatures);

return geoJsonFeatureCollection;
}

Expand Down Expand Up @@ -218,13 +228,17 @@ public static FeatureCollection fromGeoJsonFeatureCollection(String strGeoJsonFe
* @return FeatureCollection
*/
public static FeatureCollection fromGeoJsonFeatureCollection(GeoJsonObject.FeatureCollection geoJsonFeatureCollection, GeometryFactory geometryFactory) {
FeatureCollection featureCollection = new FeatureCollection();

featureCollection.setHeaders(geoJsonFeatureCollection.getHeaders());

List<Feature> features = new ArrayList<>(geoJsonFeatureCollection.getFeatures().length);
for (int i = 0; i < geoJsonFeatureCollection.getFeatures().length; i++) {
Feature feature = fromGeoJsonFeature(geoJsonFeatureCollection.getFeatures()[i], geometryFactory);
features.add(feature);
}
FeatureCollection featureCollection = new FeatureCollection();
featureCollection.setFeatures(features);

return featureCollection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public static byte[] feature2Proto(Feature feature) {
return featureCollection2Proto(fc);
}

private static ProtoFeature.Map.Builder putPropertiesToCell(Map<String, Object> properties,ToProtoKeyValueCell keyValueCell) {
private static ProtoFeature.Map.Builder putPropertiesToCell(Map<String, Object> properties, ToProtoKeyValueCell keyValueCell) {
ProtoFeature.Map.Builder propertiesBuilder = ProtoFeature.Map.newBuilder();
properties.forEach((k, v) -> {
if (null == v) {
Expand All @@ -570,23 +570,31 @@ private static ProtoFeature.Map.Builder putPropertiesToCell(Map<String, Object>
* @return ProtoFeature bytes
*/
public static byte[] featureCollection2Proto(FeatureCollection featureCollection) {
ToProtoKeyValueCell keyValueCell = new ToProtoKeyValueCell();//收集key-value与id对应关系
ProtoFeature.FeatureCollection.Builder builder = ProtoFeature.FeatureCollection.newBuilder();
for (Feature feature : featureCollection.getFeatures()) {
//properties转换
Map<String, Object> properties = feature.getProperties();
if (null != properties) {
ProtoFeature.Map.Builder propertiesBuilder = putPropertiesToCell(properties, keyValueCell);
builder.addPropertiess(propertiesBuilder);
} else {
builder.addPropertiess(nullMap);
ToProtoKeyValueCell keyValueCell = new ToProtoKeyValueCell();//收集key-value与id对应关系
//转换头信息
if (null != featureCollection.getHeaders()) {
ProtoFeature.Map.Builder propertiesBuilder = putPropertiesToCell(featureCollection.getHeaders(), keyValueCell);
builder.setHeaders(propertiesBuilder);
}
//转换features
if (null != featureCollection.getFeatures()) {
for (Feature feature : featureCollection.getFeatures()) {
//properties转换
Map<String, Object> properties = feature.getProperties();
if (null != properties) {
ProtoFeature.Map.Builder propertiesBuilder = putPropertiesToCell(properties, keyValueCell);
builder.addPropertiess(propertiesBuilder);
} else {
builder.addPropertiess(nullMap);
}
//geometry转换
Geometry geometry = feature.getGeometry();
ProtoFeature.Geometry.Builder geometryBuilder = geometry2ProtoBuilder(geometry);
builder.addGeometries(geometryBuilder);
}
//geometry转换
Geometry geometry = feature.getGeometry();
ProtoFeature.Geometry.Builder geometryBuilder = geometry2ProtoBuilder(geometry);
builder.addGeometries(geometryBuilder);

}

keyValueCell.toProto(builder);
return builder.build().toByteArray();
}
Expand Down Expand Up @@ -988,9 +996,18 @@ public static FeatureCollection proto2featureCollection(byte[] bytes, GeometryFa
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
FeatureCollection featureCollection = new FeatureCollection();

//构造Properties 真实值
FromProtoKeyValueCell keyValueCell = new FromProtoKeyValueCell(pFeatureCollection);

//构造headers
if (pFeatureCollection.hasHeaders()) {
ProtoFeature.Map pHeaders = pFeatureCollection.getHeaders();
Map<String, Object> headers = keyValueCell.parseProperties(pHeaders);
featureCollection.setHeaders(headers);
}

//构造feature
int featureNum = pFeatureCollection.getGeometriesCount();
ArrayList<Feature> features = new ArrayList<>(featureNum);
Expand All @@ -1007,9 +1024,8 @@ public static FeatureCollection proto2featureCollection(byte[] bytes, GeometryFa

features.add(feature);
}

FeatureCollection featureCollection = new FeatureCollection();
featureCollection.setFeatures(features);

return featureCollection;
}

Expand Down
Loading

0 comments on commit 159f345

Please sign in to comment.