【util】GeoJSON标准格式验证

该博客介绍了如何使用Java实现GeoJSON格式的验证。GeoUtil类提供了验证GeoJSON是否符合标准的方法,通过尝试将输入转换为不同的GeoJSON类型来判断。博客中还包含了单元测试用例,展示了一些有效和无效的GeoJSON示例。此外,提到了相关依赖项和GEOJSON的标准格式。
摘要由CSDN通过智能技术生成

针对下面存储格式,进行验证是否允许存储。

格式验证

在这里插入图片描述

临时实践

  • GeoUtil

import cn.hutool.json.JSONUtil;
import com.github.filosganga.geogson.gson.GeometryAdapterFactory;
import com.github.filosganga.geogson.model.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GeoUtil {
	/**
	 * 验证geojson[不符合],true表示格式不符合
	 *
	 * @param geoJson
	 * @return
	 */
	public static boolean verifyNotGeoJson(String geoJson) {
		return !verifyGeoJson(geoJson);
	}

	/**
	 * 验证geojson[符合],true表示格式符合
	 *
	 * @param geoJson
	 * @return
	 */
	public static boolean verifyGeoJson(String geoJson) {
		if (!JSONUtil.isJson(geoJson)) {
			return false;
		}
		Gson gson = new GsonBuilder()
				.registerTypeAdapterFactory(new GeometryAdapterFactory())
				.create();
		try {
			//FeatureCollection featureCollection = //标准格式
			gson.fromJson(geoJson, FeatureCollection.class);
			return true;
		} catch (Exception e) {
		}
		try {
			//Feature feature = //标准格式
			gson.fromJson(geoJson, Feature.class);
			return true;
		} catch (Exception e) {
		}
		try {
			//Point point = //点要素
			gson.fromJson(geoJson, Point.class);
			return true;
		} catch (Exception e) {
		}
		try {
			//MultiPolygon multiPolygon = //多点要素
			gson.fromJson(geoJson, MultiPolygon.class);
			return true;
		} catch (Exception e) {
		}

		try {
			//LineString lineString = //线要素
			gson.fromJson(geoJson, LineString.class);
			return true;
		} catch (Exception e) {
		}
		try {
			//MultiLineString multiLineString = //多线要素
			gson.fromJson(geoJson, MultiLineString.class);
			return true;
		} catch (Exception e) {
		}
		try {
			//Polygon polygon = //多边形Polygon
			gson.fromJson(geoJson, Polygon.class);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
}

单元测试

  • GeoUtilTest
public class GeoUtilTest {

	@Test
	public void verifyGeoJson() throws IOException {
		String geoJson = "";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));
		geoJson = "{}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));
		geoJson = "{\"name\":\"xxx\"}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));

		//标准格式
		geoJson = "{\n" +
				"  \"type\": \"FeatureCollection\",\n" +
				"  \"features\": [\n" +
				"        {\"type\":\"Feature\",\n" +
				"        \"properties\":{},\n" +
				"        \"geometry\":{\n" +
				"            \"type\":\"Point\",\n" +
				"            \"coordinates\":[105.380859375,31.57853542647338]\n" +
				"            }\n" +
				"        }\n" +
				"    ]\n" +
				"}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));
		//shp图层导入的一个库表脚本
		geoJson = "{\"bbox\":[-61.88722200000001,17.024441000000152,-61.686668000000026,17.703888000000077],\"type\":\"MultiPolygon\",\"coordinates\":[[[[-61.686668000000026,17.024441000000152],[-61.88722200000001,17.105273999999966],[-61.79444899999993,17.1633300000001],[-61.686668000000026,17.024441000000152]]],[[[-61.72917199999989,17.608608000000046],[-61.853057999999976,17.583054000000104],[-61.873062000000004,17.703888000000077],[-61.72917199999989,17.608608000000046]]]]}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));

		geoJson = "{\n" +
				"  \"type\": \"FeatureCollection\",\n" +
				"  \"features\": []\n" +
				"}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));

		geoJson = "{\"type\":\"Feature\",\n" +
				"    \"properties\":{},\n" +
				"    \"geometry\":{\n" +
				"        \"type\":\"Point\",\n" +
				"        \"coordinates\":[105.380859375,31.57853542647338]\n" +
				"        }\n" +
				"}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));

		geoJson = "{\"type\":\"Feature\",\n" +
				"    \"properties\":{},\n" +
				"    \"geometry\":{\n" +
				"        \"type\":\"MultiPoint\",\n" +
				"        \"coordinates\":[[105.380859375,31.57853542647338],\n" +
				"                [105.580859375,31.52853542647338]\n" +
				"            ]\n" +
				"        }\n" +
				"}";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));

		geoJson = "{\"type\":\"Feature\",\n" +
				"    \"properties\":{},\n" +
				"    \"geometry\":{\n" +
				"        \"type\":\"LineString\",\n" +
				"        \"coordinates\":[[105.6005859375,30.65681556429287],\n" +
				"        [107.95166015624999,31.98944183792288],\n" +
				"        [109.3798828125,30.031055426540206],\n" +
				"        [107.7978515625,29.935895213372444]]\n" +
				"        }\n" +
				"    }";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));


		geoJson = "{\"type\":\"Feature\",\n" +
				"    \"properties\":{},\n" +
				"    \"geometry\":{\n" +
				"        \"type\":\"MultiLineString\",\n" +
				"        \"coordinates\":\n" +
				"        [\n" +
				"            [\n" +
				"                [105.6005859375,30.65681556429287],\n" +
				"                [107.95166015624999,31.98944183792288],\n" +
				"                [109.3798828125,30.031055426540206],\n" +
				"                [107.7978515625,29.935895213372444]\n" +
				"            ],\n" +
				"            [\n" +
				"                [109.3798828125,30.031055426540206],\n" +
				"                [107.1978515625,31.235895213372444]\n" +
				"            ]\n" +
				"        ]\n" +
				"                }\n" +
				"    }";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));


		geoJson = "{\"type\":\"Feature\",\n" +
				"    \"properties\":{},\n" +
				"    \"geometry\":{\n" +
				"        \"type\":\"Polygon\",\n" +
				"        \"coordinates\":[\n" +
				"                        [\n" +
				"                          [106.10595703125,33.33970700424026],\n" +
				"                          [106.32568359375,32.41706632846282],\n" +
				"                          [108.03955078125,32.2313896627376],\n" +
				"                          [108.25927734375,33.15594830078649],\n" +
				"                          [106.10595703125,33.33970700424026]\n" +
				"                        ]\n" +
				"                      ]\n" +
				"        }\n" +
				"    }";
		System.out.println(GeoUtil.verifyGeoJson(geoJson));
	}

}

版本依赖

		<dependency>
        	<groupId>cn.hutool</groupId>
        	<artifactId>hutool-all</artifactId>
        	<version>5.5.7</version>
		</dependency>
		<dependency>
			<groupId>com.github.filosganga</groupId>
			<artifactId>geogson-core</artifactId>
			<version>1.2.21</version>
		</dependency>

参考来源

更具体的内容参考rfc7946

GEOJSON标准格式学习

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

掘金者说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值