如何在要素集合中添加高度坐标然后序列化为 Geojson
How to add altitude coordinates in Feature Collection then serialize to Geojson
非常感谢您的帮助,我使用 GeoJson.Net 将 GeoJson 文件反序列化为特征集合。然后我从特征集合中获取特征。在功能内部,我可以循环并获取所有类型的几何体。从那里我可以循环每种类型并获取坐标。
但是现在我坚持如何编辑为每个坐标添加等于 0 的海拔高度然后序列化回 geojson。
这是我的示例 test.geojson 文件:
{
"type": "FeatureCollection",
"name": "MYS_adm2",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"id": 0,
"properties": {"party": "Republican","count": "2500"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
},
{
"type": "Feature",
"id": 1,
"properties": {"party": "Democrat","count": "1300"},
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]]
}
}
]
}
这是我反序列化的代码:
string filePath = @"D:\test.geojson";
FeatureCollection collection = JsonConvert.DeserializeObject<FeatureCollection>(File.ReadAllText(filePath)); //1 min 21 s
var feature = collection.Features;
foreach (var featureItem in feature)
{
if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Point))
{
Point point = featureItem.Geometry as Point;
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPoint))
{
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Polygon))
{
Polygon polygon = featureItem.Geometry as Polygon;
foreach (var Item in polygon.Coordinates)
{
foreach (var coordinates in Item.Coordinates)
{
//Here I want to add value 0 altitude coordinate
}
}
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPolygon))
{
MultiPolygon multiPolygon = featureItem.Geometry as MultiPolygon;
foreach (var Item in multiPolygon.Coordinates)
{
foreach (var item1 in Item.Coordinates)
{
foreach (var item2 in item1.Coordinates)
{
//Here I want to add value 0 altitude coordinate
}
}
}
}
}
//here I want to serialize my FeatureCollection after edit
File.WriteAllText(@"D:\test_Edit.geojson", JsonConvert.SerializeObject(collection));
我的预期输出是添加等于 0 的 Altitude,如下所示:
{
"type": "FeatureCollection",
"name": "MYS_adm2",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"id": 0,
"properties": {"party": "Republican","count": "2500"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99, 0],
[-97.22, 48.98, 0],
[-96.58, 45.94, 0],
[-104.03, 45.94, 0],
[-104.05, 48.99, 0]
]]
}
},
{
"type": "Feature",
"id": 1,
"properties": {"party": "Democrat","count": "1300"},
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[
[-109.05, 41.00, 0],
[-102.06, 40.99, 0],
[-102.03, 36.99, 0],
[-109.04, 36.99, 0],
[-109.05, 41.00, 0]
]]]
}
}
]
}
类型和某些属性已禁止修改,很可能是为了保持数据完整性。因此,要改变一些属性的值,需要重新创建。更改您的版本后,我得到以下代码:
static void Main(string[] args)
{
string filePath = @"C:\Users\aleks\Documents\test.geojson.txt";
FeatureCollection collection = JsonConvert.DeserializeObject<FeatureCollection>(File.ReadAllText(filePath)); //1 min 21 s
var feature = collection.Features;
var editedFeatures = new List<Feature>();
IGeometryObject editedObject = null;
foreach (var featureItem in feature)
{
if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Point))
{
Point point = featureItem.Geometry as Point;
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPoint))
{
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Polygon))
{
var polygon = featureItem.Geometry as Polygon;
var editedLines = new List<LineString>();
foreach (var Item in polygon.Coordinates)
{
var editedcoordinates = new List<Position>();
foreach (var coordinates in Item.Coordinates)
{
editedcoordinates.Add(new Position(coordinates.Latitude, coordinates.Longitude, 0));
}
editedLines.Add(new LineString(editedcoordinates));
}
editedObject = new Polygon(editedLines);
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPolygon))
{
MultiPolygon multiPolygon = featureItem.Geometry as MultiPolygon;
var editedPoligons = new List<Polygon>();
foreach (var Item in multiPolygon.Coordinates)
{
var editedLines = new List<LineString>();
foreach (var item1 in Item.Coordinates)
{
var editedcoordinates = new List<Position>();
foreach (var item2 in item1.Coordinates)
{
editedcoordinates.Add(new Position(item2.Latitude, item2.Longitude, 0));
}
editedLines.Add(new LineString(editedcoordinates));
}
editedPoligons.Add(new Polygon(editedLines));
}
editedObject = new MultiPolygon(editedPoligons);
}
if (editedObject != null)
{
editedFeatures.Add(new Feature(editedObject, featureItem.Properties, featureItem.Id));
}
}
var editedCollection = new FeatureCollection(editedFeatures);
editedCollection.CRS = collection.CRS;
editedCollection.BoundingBoxes = collection.BoundingBoxes;
//here I want to serialize my FeatureCollection after edit
File.WriteAllText(@"C:\Users\aleks\Documents\test_Edit.geojson", JsonConvert.SerializeObject(editedCollection));
}
非常感谢您的帮助,我使用 GeoJson.Net 将 GeoJson 文件反序列化为特征集合。然后我从特征集合中获取特征。在功能内部,我可以循环并获取所有类型的几何体。从那里我可以循环每种类型并获取坐标。 但是现在我坚持如何编辑为每个坐标添加等于 0 的海拔高度然后序列化回 geojson。
这是我的示例 test.geojson 文件:
{
"type": "FeatureCollection",
"name": "MYS_adm2",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"id": 0,
"properties": {"party": "Republican","count": "2500"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
},
{
"type": "Feature",
"id": 1,
"properties": {"party": "Democrat","count": "1300"},
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]]
}
}
]
}
这是我反序列化的代码:
string filePath = @"D:\test.geojson";
FeatureCollection collection = JsonConvert.DeserializeObject<FeatureCollection>(File.ReadAllText(filePath)); //1 min 21 s
var feature = collection.Features;
foreach (var featureItem in feature)
{
if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Point))
{
Point point = featureItem.Geometry as Point;
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPoint))
{
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Polygon))
{
Polygon polygon = featureItem.Geometry as Polygon;
foreach (var Item in polygon.Coordinates)
{
foreach (var coordinates in Item.Coordinates)
{
//Here I want to add value 0 altitude coordinate
}
}
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPolygon))
{
MultiPolygon multiPolygon = featureItem.Geometry as MultiPolygon;
foreach (var Item in multiPolygon.Coordinates)
{
foreach (var item1 in Item.Coordinates)
{
foreach (var item2 in item1.Coordinates)
{
//Here I want to add value 0 altitude coordinate
}
}
}
}
}
//here I want to serialize my FeatureCollection after edit
File.WriteAllText(@"D:\test_Edit.geojson", JsonConvert.SerializeObject(collection));
我的预期输出是添加等于 0 的 Altitude,如下所示:
{
"type": "FeatureCollection",
"name": "MYS_adm2",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{
"type": "Feature",
"id": 0,
"properties": {"party": "Republican","count": "2500"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99, 0],
[-97.22, 48.98, 0],
[-96.58, 45.94, 0],
[-104.03, 45.94, 0],
[-104.05, 48.99, 0]
]]
}
},
{
"type": "Feature",
"id": 1,
"properties": {"party": "Democrat","count": "1300"},
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[
[-109.05, 41.00, 0],
[-102.06, 40.99, 0],
[-102.03, 36.99, 0],
[-109.04, 36.99, 0],
[-109.05, 41.00, 0]
]]]
}
}
]
}
类型和某些属性已禁止修改,很可能是为了保持数据完整性。因此,要改变一些属性的值,需要重新创建。更改您的版本后,我得到以下代码:
static void Main(string[] args)
{
string filePath = @"C:\Users\aleks\Documents\test.geojson.txt";
FeatureCollection collection = JsonConvert.DeserializeObject<FeatureCollection>(File.ReadAllText(filePath)); //1 min 21 s
var feature = collection.Features;
var editedFeatures = new List<Feature>();
IGeometryObject editedObject = null;
foreach (var featureItem in feature)
{
if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Point))
{
Point point = featureItem.Geometry as Point;
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPoint))
{
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Polygon))
{
var polygon = featureItem.Geometry as Polygon;
var editedLines = new List<LineString>();
foreach (var Item in polygon.Coordinates)
{
var editedcoordinates = new List<Position>();
foreach (var coordinates in Item.Coordinates)
{
editedcoordinates.Add(new Position(coordinates.Latitude, coordinates.Longitude, 0));
}
editedLines.Add(new LineString(editedcoordinates));
}
editedObject = new Polygon(editedLines);
}
else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPolygon))
{
MultiPolygon multiPolygon = featureItem.Geometry as MultiPolygon;
var editedPoligons = new List<Polygon>();
foreach (var Item in multiPolygon.Coordinates)
{
var editedLines = new List<LineString>();
foreach (var item1 in Item.Coordinates)
{
var editedcoordinates = new List<Position>();
foreach (var item2 in item1.Coordinates)
{
editedcoordinates.Add(new Position(item2.Latitude, item2.Longitude, 0));
}
editedLines.Add(new LineString(editedcoordinates));
}
editedPoligons.Add(new Polygon(editedLines));
}
editedObject = new MultiPolygon(editedPoligons);
}
if (editedObject != null)
{
editedFeatures.Add(new Feature(editedObject, featureItem.Properties, featureItem.Id));
}
}
var editedCollection = new FeatureCollection(editedFeatures);
editedCollection.CRS = collection.CRS;
editedCollection.BoundingBoxes = collection.BoundingBoxes;
//here I want to serialize my FeatureCollection after edit
File.WriteAllText(@"C:\Users\aleks\Documents\test_Edit.geojson", JsonConvert.SerializeObject(editedCollection));
}