-
Notifications
You must be signed in to change notification settings - Fork 5
/
08-SpatialPickingHandler.ashx.cs
71 lines (65 loc) · 3.09 KB
/
08-SpatialPickingHandler.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
69
70
71
using System;
using System.Data.SQLite;
using System.Globalization;
using System.Threading.Tasks;
using System.Web;
namespace SpatialTutorial
{
/// <summary>
/// Summary description for SpatialPickHandler
/// </summary>
public class SpatialPickingHandler : HttpTaskAsyncHandler
{
public override async Task ProcessRequestAsync(HttpContext context)
{
try
{
//Parse request parameters
if (!double.TryParse(context.Request.Params["lat"], NumberStyles.Float, CultureInfo.InvariantCulture, out double lat))
throw (new ArgumentException("Invalid parameter"));
if (!double.TryParse(context.Request.Params["lng"], NumberStyles.Float, CultureInfo.InvariantCulture, out double lng))
throw (new ArgumentException("Invalid parameter"));
// Select elements containing the point, pre-filter with mbr-cache to optimize performance
var query = FormattableString.Invariant(
$@"
SELECT WorldData.Id, AsGeoJSON(Geometry), Name, Region, Area, Pop from
(SELECT * from WorldGeom
WHERE ROWID IN
(Select rowid FROM cache_WorldGeom_Geometry WHERE
mbr = FilterMbrIntersects({lng}, {lat}, {lng}, {lat}))
AND Intersects(Geometry, MakePoint({lng}, {lat}))) as g
JOIN WorldData on WorldData.Id = g.Id
");
using (var command = new SQLiteCommand(query, Global.cn))
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
int id = reader.GetInt32(0);
string str = reader.GetString(1);
string name = reader.GetString(2);
string region = reader.GetString(3);
double area = reader.GetDouble(4);
double pop = reader.GetDouble(5);
// build response
context.Response.ContentType = "text/json";
context.Response.Write(string.Format(CultureInfo.InvariantCulture,
@"{{""geometry"": {0},""type"": ""Feature""," +
@"""properties"": {{""name"": ""{1}"", ""region"": ""{2}"", ""area"": ""{3}"", ""pop"": ""{4}""}}}}",
str, name, region, area, pop));
return;
}
}
// no result - return empty json
context.Response.ContentType = "text/json";
context.Response.Write("{}");
}
catch (Exception ex)
{
// exception - return error
context.Response.ContentType = "text/json";
context.Response.Write(@"{ ""error"": """ + ex + @"""}");
}
}
}
}