Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

geometry - Convert GeoData to GeoJson in c# with linq

this is my entity :

public class TLandPoint
{
    [Key]
    public int gid { get; set; }
    public Point geom { get; set; }
    public long LandId { get; set; }
    [MaxLength(150)]
    public long SumSpace { get; set; }
    public long CLevelId { get; set; }
}

and i select data from database : TLandPointDto same entity in my class

 var TLandPt = DBcontext.TLandPointSelect(p => new TLandPointDto
            {
                gid = p.gid,
                CLevelId = p.CLevelId,
                geom = (p.geom),
                LandId = p.LandId,
                SumSpace = p.SumSpace,
            }).ToList();

and this rows show List of data

now

i want create geojson from result of linq same this list

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": 6,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    5721495.610660064,
                    4222076.000807296
                ]
            },
            "properties": {
                "LandId": 5698,
                "SumSpace": 17335,
                "CLevelId": 12303020001
            }
        }
    ]
}

How to convert Linq result to geojson in c# .net core 5

question from:https://stackoverflow.com/questions/65925607/convert-geodata-to-geojson-in-c-sharp-with-linq

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create a class like this

namespace CTLandPt 
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Temperatures
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("features")]
        public List<Feature> Features { get; set; }
    }

    public partial class Feature
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("geometry")]
        public Geometry Geometry { get; set; }

        [JsonProperty("properties")]
        public Properties Properties { get; set; }
    }

    public partial class Geometry
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("coordinates")]
        public List<double> Coordinates { get; set; }
    }

    public partial class Properties
    {
        [JsonProperty("LandId")]
        public long LandId { get; set; }

        [JsonProperty("SumSpace")]
        public long SumSpace { get; set; }

        [JsonProperty("CLevelId")]
        public long CLevelId { get; set; }
    }
}

Then just map or copy the result of TLandPt to a new object of the CTLandPt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...