栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【util】GeoJSON标准格式验证

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【util】GeoJSON标准格式验证

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

格式验证

临时实践
  • 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 {
	
	public static boolean verifyNotGeoJson(String geoJson) {
		return !verifyGeoJson(geoJson);
	}

	
	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));
	}

}
版本依赖
		
        	cn.hutool
        	hutool-all
        	5.5.7
		
		
			com.github.filosganga
			geogson-core
			1.2.21
		
参考来源

更具体的内容参考rfc7946

GEOJSON标准格式学习

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/868166.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号