format~GeoJSON()

GeoJSON解析器。负责对GeoJSON格式数据的读写。

Summary

Methods:
isValidType

检测GeoJSON对象的类型是否与指定类型一致。当type是“Geometry”时,geoJson的type是“Point”,“MultiPoint”,“LineString”,“MultiLineString”,
“Polygon”,“MultiPolygon”和“GeometryCollection”中的一种返回true,否则返回false;当type是其他类型时,判断geoJson的type是否和type相同,相同返回true,
不同返回false。

read

解析GeoJson对象,支持所有标准的GeoJson对象,即对象的type属性可以为"Feature","FeatureCollection","GeometryCollection","MultiPolygon","Polygon","MultiLineString","LineString","Point","MultiPoint"。

readCollectionFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有集合几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

readFeature

从GeoJSON格式字符串或GeoJSON对象中解析出单个地理要素。

readFeatureFromObject

从GeoJSON对象中解析出单个地理要素。

readFeatures

从GeoJSON格式字符串或GeoJSON对象解析出地理要素数组(注意:GeoJSON类型定义中不包含线环(LinearRing)类型)。

readFeaturesByGeometryType

从GeoJSON对象中解析出指定类型的地理要素数组。

readFeaturesFromObject

从GeoJSON对象中解析出地理要素数组。

readGeometry

从GeoJSON格式字符串或GeoJSON对象中解析出几何体。

readGeometryFromObject

从GeoJSON对象中解析出几何体。

readLineFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有线状几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

readMLineFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有多线几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

readMPointFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有多点几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

readMPolyFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有多面几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

readPointFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有点状几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

readPolyFeatures

从GeoJSON格式字符串或GeoJSON对象中解析出含有面状几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

writeFeature

把一个地理要素解析成"Feature"类型的GeoJSON格式字符串。

writeFeatureObject

把一个地理要素转换成"Feature"类型的GeoJSON格式的数据。

writeFeatures

把一个地理要素数组解析成"FeatureCollection"类型的GeoJSON格式字符串。

writeFeaturesObject

把一个地理要素数组解析成"FeatureCollection"类型的GeoJSON格式的数据。

writeGeometry

把一个几何体转化成GeoJSON格式对象。

Constructor

new GeoJSON()

Example
let geoJSON = new hmap.format.GeoJSON();

Methods

isValidType(geoJson, type) → {Boolean}

检测GeoJSON对象的类型是否与指定类型一致。当type是“Geometry”时,geoJson的type是“Point”,“MultiPoint”,“LineString”,“MultiLineString”,
“Polygon”,“MultiPolygon”和“GeometryCollection”中的一种返回true,否则返回false;当type是其他类型时,判断geoJson的type是否和type相同,相同返回true,
不同返回false。

Parameters:
Name Type Description
geoJson Object

GeoJSON对象。

type String

类型

Returns:
Boolean -

为true时表示该对象类型与给定类型一致;为false则不一致。

Example
let geoJSON = new hmap.format.GeoJSON();
let jsonObj ={
 "type": "FeatureCollection",
 "features": [
   { "type": "Feature",
     "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
     "properties": {"prop0": "value0"}
   },
   { "type": "Feature",
     "geometry": {
       "type": "LineString",
       "coordinates": [
          [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
       ]
      },
    "properties": {
        "prop0": "value0",
        "prop1": 0.0
     }
 },
]
};
let isValid = geoJSON.isValidType(jsonObj,"FeatureCollection"); //isValid=true
let jsonObj2 = {
  "type": "Feature",
  "geometry": {
     "type": "Polygon",
     "coordinates": [
         [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
         [100.0, 1.0], [100.0, 0.0] ]
         ]
   },     *
  "properties": {
      "prop0": "value0",
     "prop1": {"this": "that"}
   }
};
isValid = geoJSON.isValidType(jsonObj2,"Geometry"); //isValid=false

read(geoJson) → {Array.<module:feature~Vector>}

解析GeoJson对象,支持所有标准的GeoJson对象,即对象的type属性可以为"Feature","FeatureCollection","GeometryCollection","MultiPolygon","Polygon","MultiLineString","LineString","Point","MultiPoint"。

Parameters:
Name Type Description
geoJson Object

GeoJson对象

Returns:
Array.<module:feature~Vector> -

geojson中解析出来的地理要素数组

Example
let geoJSON = new hmap.format.GeoJSON();
// Point类型的GeoJson对象
let exampleData1 = {
  "type": "Point",
  "coordinates": [
      120.223163,
      30.222204
    ]
 };
 let featureArray1 = geoJSON.read(exampleData1);

//“FeatureCollection”类型的GeoJSON对象
let exampleData2 = {
    "type": "FeatureCollection",
   "features": [{
        "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5, 0]
       },
       "properties": {
           "prop0": "value0"
       }
   }]
};
let featureArray2 = geoJSON.read(exampleData2);

readCollectionFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有集合几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

readFeature(geoJson, filter) → {module:feature~Vector|null}

从GeoJSON格式字符串或GeoJSON对象中解析出单个地理要素。

Parameters:
Name Type Description
geoJson String | Object

“Feature”类型的GeoJSON格式字符串或GeoJSON对象,当类型是字符串时,可以使用第二个参数指定的过滤函数进行过滤。

filter function

可选项。过滤函数,处理JSON对象中每个键值对,过滤函数返回的结果将替代原对象中的值。

Returns:
module:feature~Vector | null -

地理要素。如果解析出错,则返回null。

Example
let geoJSON = new hmap.format.GeoJSON();
// “Feature”类型的GeoJSON格式字符串
const exampleData = `{
        "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5, 0]
       },
       "properties": {
           "prop0": "value0"
       }
}`;
let result = geoJSON.readFeature(exampleData, function(key, value){
        if(key == "properties"){
             value.name = "point";
             return value;
        }
});

readFeatureFromObject(geoJsonObject) → {module:feature~Vector|null}

从GeoJSON对象中解析出单个地理要素。

Parameters:
Name Type Description
geoJsonObject Object

“Feature”类型的GeoJSON对象。

Returns:
module:feature~Vector | null -

地理要素。如果geoJsonObject的几何信息解析错误,则返回null。

Example
let geoJSON = new hmap.format.GeoJSON();
// “Feature”类型的GeoJSON对象
const exampleData = {
        "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5, 0]
       },
       "properties": {
           "prop0": "value0"
       }
};
let result = geoJSON.readFeaturesFromObject(exampleData);

readFeatures(geoJson, filter) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象解析出地理要素数组(注意:GeoJSON类型定义中不包含线环(LinearRing)类型)。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象,当为字符串格式时,可通过filter函数进行过滤。
当不是FeatureCollection类型时,返回空数组。

filter function

可选项。过滤函数,处理JSON对象中每个键值对,过滤函数返回的结果将替代原对象中的值。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

Example
let geoJSON = new hmap.format.GeoJSON();
//“FeatureCollection”类型的GeoJSON对象
const exampleData1 = {
    "type": "FeatureCollection",
   "features": [{
        "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5, 0]
       },
       "properties": {
           "prop0": "value0"
       }
   }]
};
let result = geoJSON.readFeatures(exampleData1);

readFeaturesByGeometryType(geoJsonObject, type) → {Array.<module:feature~Vector>}

从GeoJSON对象中解析出指定类型的地理要素数组。

Parameters:
Name Type Description
geoJsonObject Object

“FeatureCollection”类型的GeoJSON对象,如果不是“FeatureCollection”类型,返回空数组。

type String

地理要素的几何类型,可以是“Point”,“MultiPoint”,“LineString”,“MultiLineString”,“Polygon”,“MultiPolygon”中的一种,
其他类型将返回空数组。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

Example
let geoJSON = new hmap.format.GeoJSON();
// “FeatureCollection”类型的GeoJSON对象
const exampleData = {
    "type": "FeatureCollection",
   "features": [{
        "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5, 0]
       },
       "properties": {
           "prop0": "value0"
       }
   }]
};
let result = geoJSON.readFeaturesByGeometryType(exampleData,"Point");

readFeaturesFromObject(geoJsonObject) → {Array.<module:feature~Vector>}

从GeoJSON对象中解析出地理要素数组。

Parameters:
Name Type Description
geoJsonObject Object

“FeatureCollection”类型的GeoJSON对象。当参数不是FeatureCollection时,返回空数组。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

Example
let geoJSON = new hmap.format.GeoJSON();
// “FeatureCollection”类型的GeoJSON对象
const exampleData1 = {
    "type": "FeatureCollection",
   "features": [{
        "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5, 0]
       },
       "properties": {
           "prop0": "value0"
       }
   }]
};
let result = geoJSON.readFeaturesFromObject(exampleData1);

readGeometry(geoJson, filter) → {module:geom~Geometry|null}

从GeoJSON格式字符串或GeoJSON对象中解析出几何体。

Parameters:
Name Type Description
geoJson String | Object

GeoJSON格式字符串或GeoJSON对象,支持的类型包括:“Point”,“MultiPoint”,“LineString”,“MultiLineString”,“Polygon”,“MultiPolygon”和“GeometryCollection”。

filter function

可选项。过滤函数,处理JSON对象中每个键值对,过滤函数返回的结果将替代原对象中的值。

Returns:
module:geom~Geometry | null -

几何体。如果解析出错,则返回null。

Example
let geoJSON = new hmap.format.GeoJSON();
// Point类型的GeoJSON格式字符串
let geomObj = `{
  "type": "Point",
  "coordinates": [
      120.223163,
      30.222204
   ]
}`;
let geom = geoJSON.readGeometryFromObject(geomObj);
let feature = new hmap.feature.Vector(geom);

readGeometryFromObject(geoJsonObject) → {module:geom~Geometry|null}

从GeoJSON对象中解析出几何体。

Parameters:
Name Type Description
geoJsonObject Object

GeoJSON对象,支持的类型包括:“Point”,“MultiPoint”,“LineString”,“MultiLineString”,“Polygon”,“MultiPolygon”和“GeometryCollection”。

Throws:

当GeoJSON对象坐标数组或者GeoJSON对象类型不支持时,抛出异常

Returns:
module:geom~Geometry | null -

几何体。如果参数geoJsonObject为null,返回空对象null。

Example
let geoJSON = new hmap.format.GeoJSON();
// Point类型的GeoJSON对象
let geomObj = {
  "type": "Point",
  "coordinates": [
      120.223163,
       30.222204
   ]
 };
 let geom = geoJSON.readGeometryFromObject(geomObj);
 let feature = new hmap.feature.Vector(geom);

readLineFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有线状几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

readMLineFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有多线几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

readMPointFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有多点几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

readMPolyFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有多面几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

readPointFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有点状几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

readPolyFeatures(geoJson) → {Array.<module:feature~Vector>}

从GeoJSON格式字符串或GeoJSON对象中解析出含有面状几何体的地理要素数组。
结果以地理要素数组的形式返回,数组中要素的几何体类型相同。

Parameters:
Name Type Description
geoJson String | Object

“FeatureCollection”类型的GeoJSON格式字符串或GeoJSON对象。

Returns:
Array.<module:feature~Vector> -

地理要素数组。

writeFeature(feature) → {String}

把一个地理要素解析成"Feature"类型的GeoJSON格式字符串。

Parameters:
Name Type Description
feature module:feature~Vector

地理要素。

Returns:
String -

GeoJSON格式字符串。

Example
let geoJSON = new hmap.format.GeoJSON();
let point = new hmap.geom.Point(new hmap.basetype.Coordinate(120,30,0));
let feature = new hmap.feature.Vector(point);
let result = geoJSON.writeFeature(feature);

writeFeatureObject(feature) → {Object}

把一个地理要素转换成"Feature"类型的GeoJSON格式的数据。

Parameters:
Name Type Description
feature module:feature~Vector

地理要素。

Returns:
Object -

GeoJSON格式的数据。

Example
let geoJSON = new hmap.format.GeoJSON();
const point = new hmap.geom.Point(new hmap.basetype.Coordinate(120,30,0));
let feature = new hmap.feature.Vector(point, {'name':'name1','type':'type1'});
let result = geoJSON.writeFeatureObject(feature);

writeFeatures(features) → {String}

把一个地理要素数组解析成"FeatureCollection"类型的GeoJSON格式字符串。

Parameters:
Name Type Description
features Array.<module:feature~Vector>

地理要素数组。

Returns:
String -

GeoJSON格式字符串。

Example
let coord1 = new hmap.basetype.Coordinate(120,30,0);
let coord2 = new hmap.basetype.Coordinate(121,31,0);
let point1 = new hmap.geom.Point(coord1);
let point2 = new hmap.geom.Point(coord2);
let feature1 = new hmap.feature.Vector(point1);
let feature2 = new hmap.feature.Vector(point2);
let geoJSON = new hmap.format.GeoJSON();
let result = geoJSON.writeFeatures([feature1,feature2]);

writeFeaturesObject(features) → {Object}

把一个地理要素数组解析成"FeatureCollection"类型的GeoJSON格式的数据。

Parameters:
Name Type Description
features Array.<module:feature~Vector>

地理要素数组。

Throws:

当参数features数组中的元素不是地理要素时,抛出异常

Returns:
Object -

GeoJSON格式的数据。

Example
let coord1 = new hmap.basetype.Coordinate(120,30,0);
let coord2 = new hmap.basetype.Coordinate(121,31,0);
let point1 = new hmap.geom.Point(coord1);
let point2 = new hmap.geom.Point(coord2);
let feature1 = new hmap.feature.Vector(point1);
let feature2 = new hmap.feature.Vector(point2);
let geoJSON = new hmap.format.GeoJSON();
let result = geoJSON.writeFeaturesObject([feature1,feature2]);

writeGeometry(geom) → {Object}

把一个几何体转化成GeoJSON格式对象。

Parameters:
Name Type Description
geom module:geom~Geometry

几何体对象。

Returns:
Object -

GeoJSON对象。

Example
let geoJSON = new hmap.format.GeoJSON();
let coord = new hmap.basetype.Coordinate(120,30,0);
let point = new hmap.geom.Point(coord);
let result = geoJSON.writeGeometry(point);