Skip to content

Instantly share code, notes, and snippets.

@mstefarov
Last active May 1, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstefarov/00fc21bae58ee564ebd90f745803ee7c to your computer and use it in GitHub Desktop.
Save mstefarov/00fc21bae58ee564ebd90f745803ee7c to your computer and use it in GitHub Desktop.
Example how ArcGISHttpClientHandler.HttpRequestBegin event can be used to rewrite WMS requests.
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