Last active
May 1, 2018 00:17
-
-
Save mstefarov/00fc21bae58ee564ebd90f745803ee7c to your computer and use it in GitHub Desktop.
Example how ArcGISHttpClientHandler.HttpRequestBegin event can be used to rewrite WMS requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void ReplaceCrs84WithEpsg4326(object sender, HttpRequestMessage e) | |
{ | |
string rawQuery = e.RequestUri.Query; | |
// Skip requests without a query | |
if (string.IsNullOrEmpty(rawQuery)) | |
return; | |
// For convenience, parse query as a Dictionary | |
var queryParameters = System.Web.HttpUtility.ParseQueryString(rawQuery); | |
var queryDict = queryParameters.AllKeys.ToDictionary( | |
k => k.ToUpperInvariant(), | |
k => queryParameters[k].ToUpperInvariant()); | |
// Skip requests that are not WMS | |
string serviceParam; | |
if (!queryDict.TryGetValue("SERVICE", out serviceParam) || serviceParam!= "WMS") | |
return; | |
// Skip requests that don't use CRS:84 | |
string crsParam; | |
if (!queryDict.TryGetValue("CRS", out crsParam) || crsParam != "CRS:84") | |
return; | |
// TODO: if necessary, apply additional checks here to limit which WMS services are affected | |
// Replace "CRS:84" with "EPSG:4326" | |
queryParameters["CRS"] = "EPSG:4326"; | |
// Swap lat/lon in the requested coordinates | |
var bbox = queryDict["BBOX"].Split(','); | |
var swappedBbox = bbox[1] + ',' + bbox[0] + ',' + bbox[3] + ',' + bbox[2]; | |
queryParameters["BBOX"] = swappedBbox; | |
// Replace the original query with our modified one | |
var uriWithoutParams = e.RequestUri.AbsoluteUri.Split('?').First(); | |
e.RequestUri = new Uri(uriWithoutParams + '?' + queryParameters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment