1.0.1 2024-01-27 17:25 UTC

This package is auto-updated.

Last update: 2024-09-27 18:57:02 UTC


README

安装

该库以 软件包 的形式发布,可通过 Composer 安装

composer require nikopeikrishvili/geojson

创建 GeoJson 对象

您可以创建 Feature 对象并从中提取 GeoJson 字符串,只需提供坐标即可,例如

多边形

$feature = new GeoJSON\FeatureTypes\Polygon([/**coordinates array**/]);

多边形集合

$feature = new GeoJSON\FeatureTypes\MultiPolygon([/**coordinates array**/]);

$feature = new GeoJSON\FeatureTypes\Point([/**coordinates array**/]);

多点

$feature = new GeoJSON\FeatureTypes\MultiPoint([/**coordinates array**/]);

线字符串

$feature = new GeoJSON\FeatureTypes\LineString([/**coordinates array**/]);

多线字符串

$feature = new GeoJSON\FeatureTypes\MultiLineString([/**coordinates array**/]);

生成 GeoJson 字符串

$geoJson = $feature->asGeoJson();
$geoJsonString = $geoJson->asString();
echo $geoJsonString.PHP_EOL;
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -72.04315136683675,
              29.451187895050822
            ],
            [
              -72.04315136683675,
              27.849712263460177
            ],
            [
              -68.23275413868745,
              27.849712263460177
            ],
            [
              -68.23275413868745,
              29.451187895050822
            ],
            [
              -72.04315136683675,
              29.451187895050822
            ]
          ]
        ]
      }
    }
  ]
}

示例

多边形 - 创建多边形特征
- 创建点特征
多边形集合 - 创建多边形集合特征
多点 - 创建多点特征
线字符串 - 创建线字符串特征
多线字符串 - 创建多线字符串特征

GeoJson 作为对象

您可以使用此库验证 GeoJson 是否符合 RFC 7946 标准

目前,它支持以下 GeoJson 类型
多边形 - 多边形特征
- 点特征
多边形集合 - 多边形集合特征
多点 - 多点特征
线字符串 - 线字符串特征
多线字符串 - 多线字符串特征

该软件包支持将 stdclass、字符串和数组作为输入

// String Based
$point = '{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[-90,180]}}';
$geojson = new GeoJSON($point);

// Array Based
$geojson = new GeoJSON([
    'type' => 'Feature',
    'properties' => [],
    'geometry' => [
        'type' => 'Point',
        'coordinates' => [-90, 180]
    ]
]);
// StdClass Based
$point = '{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[-90,180]}}';
$geojson = new GeoJSON(json_decode($point));

注意:如果您的 geojson 以 Feature 开头而不是 FeatureCollection,则软件包将用 FeatureCollection 包装它