-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapData.ashx.cs
68 lines (58 loc) · 2.32 KB
/
MapData.ashx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
namespace WebFormsMap
{
/// <summary>
/// Summary description for MapData
/// </summary>
public class MapData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// you can apply some request parameters for filtering or styling
var someRequestParams = context.Request.Params["someRequestParams"];
var someOtherParams = context.Request.Params["someOtherParams"];
// the return type is json
context.Response.ContentType = "application/json";
// our database is simulated by a csv file, i'm using the Microsoft.VisualBasic.FileIO TextFileReader class
var parser = new TextFieldParser(HttpRuntime.AppDomainAppPath + "\\App_Data\\Baufeldt.txt");
parser.Delimiters = new string[] { ";" };
// we're just writing GeoJson brute-force, without any libraries (JSON.NET would be a good choice)
context.Response.Write(@"{""type"": ""FeatureCollection"", ""features"": ["); // start json
bool isFirstFeature = true;
while (true)
{
string[] parts = parser.ReadFields();
if (parts == null)
{
break;
}
if (!isFirstFeature)
context.Response.Write(",");
else
isFirstFeature = false;
// write coordinate
context.Response.Write(string.Format(CultureInfo.InvariantCulture,
@"{{""type"": ""Feature"", ""geometry"": {{""type"": ""Point"",""coordinates"": [{0}, {1}]}}, ",
parts[13], parts[14]));
// write attributes
context.Response.Write(string.Format(CultureInfo.InvariantCulture,
@"""properties"": {{""id"": ""{0}"", ""description"": ""{1}"", ""type"": ""{2}""}}}}",
parts[0], parts[1], parts[6]));
}
context.Response.Write("]}"); // close json
}
public bool IsReusable
{
get
{
return true;
}
}
}
}