GeoJSON详解(带图)

GeoJSON_README

GeoJSON

1 引子

GeoJSON 是用 JSON 的语法表达和存储地理数据,可以说是 JSON 的子集。
图片数据参考:http://geojson.io

  • 下面是一个点数据。
{
   
  "type": "FeatureCollection",
  "features": [
        {
   "type":"Feature",
        "properties":{
   },
        "geometry":{
   
            "type":"Point",
            "coordinates":[105.380859375,31.57853542647338]
            }
        }
    ]
}

在这里插入图片描述

2 基本要素

地理要素分为Point(点)MultiPoint(多点)LineString(线)MultiLineString(多线)Polygon(面)MultiPolygon(多面)GeometryCollection(几何集合)

这些地理要素被规划到geometry特征的type属性下。

并且不同的type会有不同的coordinates值。

"geometry":{
   
        "type":"Point",
        "coordinates":[105.380859375,31.57853542647338]
}
  • Point(点)

点要素是最简单的,类型type对应Point,然后坐标是一个1维的数组,里面有两个元素(如果是立体的坐标就是三维x,y,z),分别为经度和纬度。

coordinates的值是一个点。

"geometry":{
   
        "type":"Point",
        "coordinates":[105.380859375,31.57853542647338]
}

在这里插入图片描述

  • MultiPoint(多点)

多点,坐标数目变多。

coordinates的值是多个点。

"geometry":{
   
        "type":"MultiPoint",
        "coordinates":[[105.380859375,31.57853542647338],
                [105.580859375,31.52853542647338]
        ]
}

在这里插入图片描述

  • LineString(线)

线要素就是指线段,记录的是线的端点坐标,可视化时会按照记录顺序联结
对于曲线(如贝塞尔曲线)目前还没有很好的表达,但是在地理数据中,曲线一般会用LineString去拟合,现实地理世界中也没有标准的曲线地理要素。

线要素的坐标coordinates里的二维数组和多点要素基本一样,区别就在type上了。

coordinates的值是多个点,类似MultiPoint(多点)

"geometry"
在Java中,可以使用GeoTools库来将GeoJSON转换为图片。GeoTools是一个开源的Java库,用于处理地理空间数据。以下是实现的基本步骤: 1. 创建一个FeatureCollection对象,将GeoJSON文件中的所有Feature添加到该对象中。 2. 创建一个MapContent对象,向其中添加一个MapLayer对象。 3. 使用Style对象定义FeatureCollection的样式。 4. 使用MapImageGenerator类的generateImage()方法将FeatureCollection渲染为BufferedImage对象。 5. 使用ImageIO类将BufferedImage对象保存为图片文件。 以下是Java代码示例: ```java // 读取GeoJSON文件 File geojsonFile = new File("path/to/file.geojson"); FeatureJSON featureJSON = new FeatureJSON(); FeatureCollection featureCollection = featureJSON.readFeatureCollection(new FileInputStream(geojsonFile)); // 创建MapContent对象 MapContent mapContent = new MapContent(); mapContent.setTitle("GeoJSON to Image"); // 将FeatureCollection添加到MapContent中 MapLayer mapLayer = new FeatureLayer(featureCollection, createStyle()); mapContent.addLayer(mapLayer); // 渲染FeatureCollection为BufferedImage对象 MapImageGenerator mapImageGenerator = new StreamingRenderer().createMapImageGenerator(); mapImageGenerator.setImageSize(new Dimension(800, 600)); BufferedImage image = mapImageGenerator.generateImage(mapContent); // 保存为图片文件 File imageFile = new File("path/to/image.png"); ImageIO.write(image, "PNG", imageFile); ``` 注意,以上代码只是示例,实际使用时需要进行异常处理等适当的改动。另外,需要根据实际的需求定义FeatureCollection的样式,例如设置颜色、线宽、填充等。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值