GeoJSON简介

GeoJSON是一种用JSON格式表示地理空间数据的标准(RFC 7946),前端地图(如Leaflet、Mapbox)经常用它传递点、线、面等几何信息。


名词解释:

**RFC 7946:**GeoJSON 地理数据格式的官方国际标准,2016年发布,取代了08年发布的旧标准。

Leaflet: 是一个开源并且对移动端友好的交互式地图 JavaScript 库。 它大小仅仅只有 42 KB of JS, 并且拥有绝大部分开发者所需要的所有地图特性 ,官网:Leaflet - 一个交互式地图 JavaScript 库 (leafletjs.cn)

Mapbox: 是全球领先的地图技术服务平台‌,提供定制化地图、导航、搜索及位置数据解决方案,官网在这,有兴趣的自己看:www.mapbox.com


数据类型:

GeoJSON的基本结构如下:

java 复制代码
{
  "type": "几何类型",
  "coordinates": [坐标数据]
}

其中type有七种类型,分别是Point(点)、MultiPoint(多点)、LineString(线串)、MultiLineString(多线串)、Polygon(多边形)、MultiPolygon(多多边形)、GeometryCollection(几何集合)


数据示例:

1. Point(点)

java 复制代码
{
	"type": "Point",
	"coordinates": [113.324568, 23.106682]
}

2. MultiPoint(多点)

javascript 复制代码
{
	"type": "MultiPoint",
	"coordinates": [
		[113.32, 23.10],
		[113.33, 23.11],
		[113.34, 23.12]
	]
}

3. LineString(线串)

javascript 复制代码
{
	"type": "LineString",
	"coordinates": [
		[113.1, 23.0],
		[113.2, 23.1],
		[113.3, 23.2]
	]
}

4. MultiLineString(多线串)

java 复制代码
{
  "type": "MultiLineString",
  "coordinates": [
    [[113.1,23.0], [113.2,23.1]],
    [[113.3,23.2], [113.4,23.3]]
  ]
}
// 多条独立的路线

5. Polygon(多边形)

java 复制代码
{
	"type": "Polygon",
	"coordinates": [
		[
			[113.2, 23.0],
			[113.4, 23.0],
			[113.4, 23.2],
			[113.2, 23.2],
			[113.2, 23.0]
		]
	]
}
// 第一个数组是外环,后面的数组是内环(洞),首尾坐标必须相同

6. MultiPolygon(多多边形)

java 复制代码
{
	"type": "MultiPolygon",
	"coordinates": [
		[
			[
				[113.2, 23.0],
				[113.3, 23.0],
				[113.3, 23.1],
				[113.2, 23.1],
				[113.2, 23.0]
			]
		],
		[
			[
				[113.4, 23.2],
				[113.5, 23.2],
				[113.5, 23.3],
				[113.4, 23.3],
				[113.4, 23.2]
			]
		]
	]
}
// 多个不相连的面,如广东省各市分开的行政区划

7. GeometryCollection(几何集合)

java 复制代码
{
	"type": "GeometryCollection",
	"geometries": [{
			"type": "Point",
			"coordinates": [113.32, 23.10]
		},
		{
			"type": "LineString",
			"coordinates": [
				[113.1, 23.0],
				[113.2, 23.1]
			]
		}
	]
}
// 混合不同类型,注意这里用 "geometries" 而不是 "coordinates"

注意type 除了上述 7 种几何类型,还有两种顶层类型用于包裹:

java 复制代码
// Feature:一个几何 + 它的属性
{
	"type": "Feature",
	"geometry": { ...
	},
	"properties": {
		"name": "广州塔"
	}
}

// FeatureCollection:多个 Feature 的集合(最常见的前端传参格式)
{
	"type": "FeatureCollection",
	"features": [{ ...
	}, { ...
	}]
}

//示例数据:
//Feature(要素)
{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [113.324568, 23.106682]
  },
  "properties": {
    "name": "广州塔",
    "height": 600,
    "buildYear": 2010,
    "category": "地标"
  }
}
//geometry 里放任意几何类型,properties 里放业务属性,可以自定义任意字段。

//FeatureCollection(要素集合)
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [113.324568, 23.106682]
      },
      "properties": {
        "id": "BRIDGE_001",
        "name": "珠江大桥",
        "bridgeType": "梁桥",
        "length": 1200,
        "检测等级": "A"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [113.100, 23.000],
          [113.150, 23.050],
          [113.200, 23.100]
        ]
      },
      "properties": {
        "id": "ROAD_001",
        "name": "G107国道广州段",
        "roadClass": "国道",
        "totalLength": 15000
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [[
          [113.20, 23.00],
          [113.40, 23.00],
          [113.40, 23.20],
          [113.20, 23.20],
          [113.20, 23.00]
        ]]
      },
      "properties": {
        "id": "AREA_001",
        "name": "广州市区范围",
        "adminLevel": "市辖区"
      }
    }
  ]
}
相关推荐
小bo波20 小时前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯21 小时前
GoF设计模式——备忘录模式
java·后端·spring·设计模式
SamDeepThinking1 天前
裁掉那个差程序员后,给你看团队里高手的代码:这个习惯,希望你有
java·后端·程序员
朕瞧着你甚好1 天前
技术雷达 & Java 集成评估报告 — Apache Tika 3.3.1
java·ai编程
MacroZheng1 天前
短短几天,暴涨2.8万Star!又一款编程神器开源!
java·人工智能·后端
SamDeepThinking1 天前
函数式编程:用BiFunction消除多类型分支的代码重复
java·后端·面试
Flittly2 天前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了2 天前
Java 生成二维码解决方案
java·后端
人活一口气2 天前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
NE_STOP2 天前
Vibe Coding -- 完整项目案例实操
java