ASHX 将 SQL 数据序列化为 GeoJSON 格式

ASHX To Serialize SQL Data To GeoJSON Format

我正在尝试构建一个 Web 应用程序,它通过 ASHX 处理程序从 SQL 服务器提取数据,转换为有效的 GeoJSON 格式,并在传单地图上显示标记。我有从 SQL 服务器中提取三个字段的查询(描述、LAT、LONG)。我有能力在 Leaflet 中显示 GeoJSON 数据(来自网站文档)。我想不通的是如何成功构建 GeoJSON 数据。是否有使用 Javascript 序列化程序构建 GeoJSON 对象的简单方法,或者这是我必须在 hanlder 中构建的过程。

这是我要创建的 GeoJSON 文件示例:

{
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "name": "Placemarker 1",
        "marker-color": "#0000ff",
        "marker-symbol": "airport"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -77.12911152370515,
          38.79930767201779
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "Placemarker 2",
        "marker-color": "#FF0000",
        "marker-symbol": "hospital"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -7.12911152370515,
          5.79930767201779
        ]
      }
    }]
}

这是我目前构建一个简单的 JSON 文件的 ASHX 文件的内容:

private class DataSet
{
    public string description { get; set; }
    public double valueLat { get; set; }
    public double valueLong { get; set; }
}

public void ProcessRequest(HttpContext context)
{
    List<DataSet> listResults = new List<DataSet>();
    int recordCount = 0;

    try
    {
        // Get Connection String From WEB.CONFIG
        string connStr = ConfigurationManager.ConnectionStrings["myConnStr"].ConnectionString;

        // Connect And Get Data
        OdbcConnection sqlConn = new OdbcConnection(connStr.ToString());
        OdbcCommand sqlCmd = new OdbcCommand("{call getSampleData}", sqlConn);
        sqlConn.Open();
        OdbcDataReader rdr = sqlCmd.ExecuteReader();

        while (rdr.Read())
        {
            DataSet results = new DataSet();
            results.description = rdr["description"].ToString();
            results.valueLat = Convert.ToDouble(rdr["lat"]);
            results.valueLong = Convert.ToDouble(rdr["long"]);
            listResults.Add(results);
            recordCount++;
        }

        sqlConn.Close();
    }
    catch (OdbcException o)
    {
       context.Response.Write(o.Message.ToString());
    }

    var result = new
    {
        iTotalRecords = recordCount,
        aaData = listResults
    };

    JavaScriptSerializer js = new JavaScriptSerializer();
    context.Response.Write(js.Serialize(result));
}

这可能会帮助您从 SQL 服务器 https://blogs.msdn.microsoft.com/sqlserverstorageengine/2016/01/05/returning-spatial-data-in-geojson-format-part-1/

将数据转换为 geoJson 格式

根据评论进行编辑:

您也可以循环遍历 javascript 中的 "listResults" 并手动构建您的 geoJson

//listResults...this is dummy data, 
//but an example of what you are currently getting from SQL
var listResults = 
[{
    description:"description 1",
    valueLat: 39.8282,
    valueLong: -98.5795
},{
    description:"description 2",
    valueLat: 38.8282,
    valueLong: -97.5795
},{
    description:"description 3",
    valueLat: 37.8282,
    valueLong: -96.5795
}];

//empty geoJson collection
var geoJsonData = [];

//loop through the listResults to build individual geoJson features
for (var i = 0; i < listResults.length; i++) {
    var result = listResults[i];
    geoJsonData.push(
        {
         "type": "Feature",
         "geometry": {
         "type": "Point",
         "coordinates": [result.valueLong, result.valueLat]
         },
         "properties": {
         "description": result.description
         }
        }
    );

}

构建 geoJson 集合后,您可以使用传单使用它。