protected void Page_Load(object sender, EventArgs e) { int id = int.Parse(Request["id"]); MMTools mm = new MMTools(Context); string region = mm.GetRegionName(id); regionID = mm.GetRegionID(region); v1 = Request["v1"]; v2 = Request["v2"]; try { chartFrameSrc = "chart.aspx?id=" + Request["id"] + "&v1=" + v1 + "&v2=" + v2; dynamic ideas = mm.ReadIdeas(Context); if (v1 != null && v1 != "") { var1 = mm.GetVariable(ideas, v1, Context); if (var1 == null) { Response.End(); return; } } if (v2 != null && v2 != "") { chartFrameSrc = "loading.aspx"; var2 = mm.GetVariable(ideas, v2, Context); if (var2 == null) { Response.End(); return; } } } catch (Exception err) { Response.Write(err.Message.Replace("editbew123", "******")); Response.End(); } }
protected void Page_Load(object sender, EventArgs e) { id = Request["id"]; v1 = Request["v1"]; v2 = Request["v2"]; GSTool gs = new GSTool(Context); feature = gs.GetFeatureDB(Context, int.Parse(id)); MMTools mm = new MMTools(Context); string region = mm.GetRegionName(int.Parse(id)); regionID = mm.GetRegionID(region); if (regionID != 0) { using (HttpClient client = new HttpClient()) { Uri baseAddress = new Uri("http://app01.saeon.ac.za/nccrdapi/"); client.BaseAddress = baseAddress; //Setup post body var postBody = new { polygon = feature.wkt }; //Get response var response = client.PostAsync("odata/Projects/Extensions.ByPolygon?$expand=ProjectLocations($expand=Location($select=LatCalculated,LonCalculated))&$select=ProjectId,ProjectTitle,ProjectDescription", new StringContent(new JavaScriptSerializer().Serialize(postBody), Encoding.UTF8, "application/json")).Result; /* * if (!response.IsSuccessStatusCode) * { * String url = baseAddress + "api/Projects/GetByPolygonPost"; * throw new HttpRequestException(url + "\nPost body:" + postBody); * } */ features = response.Content.ReadAsStringAsync().Result; } } dynamic ideas = mm.ReadIdeas(Context); if (v1 != null && v1 != "") { var1 = mm.GetVariable(ideas, v1, Context); if (var1 == null) { Response.End(); return; } } if (v2 != null && v2 != "") { var2 = mm.GetVariable(ideas, v2, Context); if (var2 == null) { Response.End(); return; } } }
public ProfVar GetVariable(dynamic ideas, String id, HttpContext context) { JavaScriptSerializer js = new JavaScriptSerializer(); List <String> path = new List <string>(); dynamic idea = FindIdea(ideas, id, path); if (idea == null) { context.Response.Write("Invalid variable id: " + id); context.Response.End(); return(null); } path.Reverse(); ProfVar var = new ProfVar(); var.name = idea["title"]; var.id = idea["id"].ToString().Replace(".", "_"); foreach (String s in path) { var.path += s + "."; } int index = var.name.IndexOf("http://"); if (index == -1) { index = var.name.IndexOf("https://"); } if (index != -1) { var.link = var.name.Substring(index); var.name = var.name.Substring(0, index); } if (var.link != "") { try { String[] parts = var.link.Split('?'); String wmsurl = parts[0]; String[] args = parts[1].Split('&'); foreach (String arg in args) { String[] stuff = arg.Split('='); String key = stuff[0].ToLower(); String val = stuff[1]; if (key == "request" || key == "bbox" || key == "width" || key == "height" || key == "format" || key == "srs") { // ignore } else if (key == "layers") { var.wmslayers = val; } else if (key == "styles") { var.wmsstyles = val; } else { // add back to url if (wmsurl.IndexOf('?') == -1) { wmsurl += '?'; } else { wmsurl += '&'; } wmsurl += arg; } var.wmsurl = wmsurl; } } catch (Exception err) { context.Response.Write("Failed to parse wms url:<hr>" + var.link + "<hr>" + err.Message); } } if (idea.ContainsKey("attr")) { dynamic attr = idea["attr"]; string json = ""; if (attr.ContainsKey("note")) { dynamic note = attr["note"]; var.note = note["text"]; try { string text = var.note; int indexLink = text.IndexOf("http://"); if (indexLink == -1) { throw new Exception("Missing link in text"); } string link = text.Substring(indexLink); int indexEnd1 = link.IndexOf(' '); int indexEnd2 = link.IndexOf('\n'); if (indexEnd1 != -1 && indexEnd2 != -1 && indexEnd1 > indexEnd2) { indexEnd1 = indexEnd2; } if (indexEnd1 == -1) { throw new Exception("Missing end of link in text"); } link = link.Substring(0, indexEnd1); var.metalink = link; int indexJSON = text.IndexOf('{'); if (indexJSON == -1) { throw new Exception("no json found in note"); } json = text.Substring(indexJSON); dynamic obj = js.DeserializeObject(json); if (obj.ContainsKey("application")) { var.name = obj["application"]; } dynamic title = obj["title"]; dynamic style = obj["style"]; dynamic colormap = obj["colourmap"]; foreach (dynamic item in colormap) { dynamic dict = item; foreach (string key in dict.Keys) { String[] vals = dict[key].Split(','); String color = vals[0].Trim(); double val = 1; if (vals.Length > 1) { val = double.Parse(vals[1]); } if (color.IndexOf('#') == -1) { color = '#' + color; } color = color.ToLower(); var.dict1[color] = key; var.dict2[key] = color; // fixme // var.dictv[color] = 1; // val; if (var.dicb.ContainsKey(key) == false) { var.dicb[key] = new MMBucket(); var.dicb[key].dict = new Dictionary <string, MMBucket>(); } } } } catch (Exception err) { context.Response.Write("Failed to parse note<hr><pre>" + json + "</pre><hr>" + err.Message); return(null); } // parse metadata if (var.metalink != "") { String uid = GetURLVar(var.metalink, "uuid"); String url = "http://oa.dirisa.org/jsonGetRecords?uuid=" + uid; String org = url; String usr = "******"; String psw = "editbew123"; url += "&__ac_name=" + usr + "&__ac_password="******"count"] < 1) { throw new Exception("Record not found:" + url); } dynamic content = res["content"][0]; dynamic jsonData = content["jsonData"]; dynamic additionalFields = jsonData["additionalFields"]; dynamic onlineResources = additionalFields["onlineResources"]; dynamic onlineResource = onlineResources[0]; string href = onlineResource["href"]; try { String[] parts = href.Split('?'); String url2 = parts[0]; String[] args = parts[1].Split('&'); foreach (String arg in args) { String[] stuff = arg.Split('='); String key = stuff[0].ToLower(); String val = stuff[1]; if (key == "request" || key == "bbox" || key == "width" || key == "height" || key == "format" || key == "srs") { // ignore } else if (key == "layers") { var.wmslayers = val; } else if (key == "styles") { var.wmsstyles = val; } else { // add back to url if (url2.IndexOf('?') == -1) { url2 += '?'; } else { url2 += '&'; } url2 += arg; } var.wmsurl = url2; } } catch (Exception err) { context.Response.Write("Failed to parse wms url:<hr>" + href + "<hr>" + err.Message); } } } } } return(var); }
public void ProcessChart(int index, ProfVar var) { double x1 = feature.x1; double y1 = feature.y1; double x2 = feature.x2; double y2 = feature.y2; double mx = x2 - x1; double my = y2 - y1; String url = var.wmsurl + String.Format("&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS={0}&STYLES={1}&TILED=true&WIDTH={6}&HEIGHT={7}&CRS=EPSG:4326&BBOX={2},{3},{4},{5}", var.wmslayers, var.wmsstyles, x1, y1, x2, y2, bmpx, bmpy); String name = "temp/" + Guid.NewGuid().ToString() + ".png"; String file = Server.MapPath(name); String col = ""; if (index == 1) { file_v1 = name; } else { file_v2 = name; } // IPolygon geom = (IPolygon)GeometryFromWKT.Parse(wkt); using (WebClient wc = new WebClient()) { try { wc.DownloadFile(url, file); } catch (Exception err) { Response.Write("Error: Failed to download '" + url + "':<br>" + err.Message); Response.End(); return; } Bitmap bmp = new Bitmap(file); if (index == 1) { bmp1 = bmp; } else { bmp2 = bmp; } for (int y = 0; y < bmpy; y++) { double py = y1 + ((double)y / (double)bmpy) * my; for (int x = 0; x < bmpx; x++) { double px = x1 + ((double)x / (double)bmpx) * mx; Color mc = mask.GetPixel(x, y); if (mc.R < 10 && mc.G < 10 && mc.B < 10) { col = ToWebString(bmp.GetPixel(x, y)).ToLower(); if (var.dict1.ContainsKey(col)) { String value = var.dict1[col]; double v = var.dictv.ContainsKey(col) ? var.dictv[col] : 1; MMBucket bucket = var.dicb[value]; bucket.count += v; if (index == 2) { String col1 = ToWebString(bmp1.GetPixel(x, y)).ToLower(); if (var1.dict1.ContainsKey(col1)) { String value1 = var1.dict1[col1]; MMBucket bucket1 = var1.dicb[value1]; double v1 = var1.dictv.ContainsKey(col1) ? var1.dictv[col1] : 1; if (bucket1.dict.ContainsKey(value) == false) { bucket1.dict[value] = new MMBucket(); } bucket1.dict[value].count += v1; String col2 = ToWebString(bmp2.GetPixel(x, y)).ToLower(); if (var2.dict1.ContainsKey(col2)) { String value2 = var2.dict1[col2]; MMBucket bucket2 = var2.dicb[value2]; double v2 = var2.dictv.ContainsKey(col2) ? var2.dictv[col2] : 1; if (bucket2.dict.ContainsKey(value1) == false) { bucket2.dict[value1] = new MMBucket(); } bucket2.dict[value1].count += v2; } } } } } else { // Response.Write("no "); } } } } }
protected void Page_Load(object sender, EventArgs e) { Session.LCID = 1033; String mode = Request["mode"]; if (mode != null && mode != "") { if (mode == "savesel") { SaveSelection(); } Response.End(); return; } id = Request["id"]; v1 = Request["v1"]; v2 = Request["v2"]; GSTool gs = new GSTool(Context); feature = gs.GetFeatureDB(Context, int.Parse(id)); if (Request["v"] != null) { v = Request["v"]; } double x1 = feature.x1; double y1 = feature.y1; double x2 = feature.x2; double y2 = feature.y2; ramp = new ColorRamp(); ramp.Load(Server.MapPath("images/ramp.png")); CMapRect maprect = new CMapRect(x1, y1, x2, y2); CScreenRect scrrect = new CScreenRect(0, 0, bmpx, bmpy); CZoom zoom = new CZoom(); zoom.ZoomToFullExtent(maprect, scrrect); wkt = feature.wkt; mask = new Bitmap(bmpx, bmpy); using (Graphics gr = Graphics.FromImage(mask)) { Brush white = new SolidBrush(Color.White); Brush black = new SolidBrush(Color.Black); gr.FillRectangle(white, new Rectangle(0, 0, bmpx, bmpy)); string wkt1 = wkt; wkt1 = wkt1.Replace("POLYGON((", ""); wkt1 = wkt1.Replace("))", ""); wkt1 = wkt1.Replace("), (", "|"); GraphicsPath path = new GraphicsPath(); String[] parts = wkt1.Split('|'); foreach (String part in parts) { String[] coords = part.Split(','); List <Point> pointArray = new List <Point>(); foreach (String coord in coords) { String[] xy = coord.Trim().Split(' '); double mx = double.Parse(xy[0].Trim(), CultureInfo.InvariantCulture); double my = double.Parse(xy[1].Trim(), CultureInfo.InvariantCulture); int sx = 0; int sy = 0; zoom.Map2Screen(mx, my, out sx, out sy); pointArray.Add(new Point(sx, sy)); } path.AddPolygon(pointArray.ToArray()); } gr.FillPath(black, path); } String name = "temp/mask_" + Guid.NewGuid().ToString() + ".png"; String file = Server.MapPath(name); mask.Save(file, System.Drawing.Imaging.ImageFormat.Png); file_mask = name; MMTools mm = new MMTools(Context); dynamic ideas = mm.ReadIdeas(Context); if (v1 != null && v1 != "") { var1 = mm.GetVariable(ideas, v1, Context); if (var1 == null) { Response.End(); return; } ProcessChart(1, var1); } if (v2 != null && v2 != "") { var2 = mm.GetVariable(ideas, v2, Context); if (var2 == null) { Response.End(); return; } ProcessChart(2, var2); } // normalize double dTotalMain1 = 0; double dTotalSub1 = 0; foreach (String key in var1.dicb.Keys) { MMBucket bucket = var1.dicb[key]; dTotalMain1 += bucket.count; foreach (String sub in bucket.dict.Keys) { dTotalSub1 += bucket.dict[sub].count; } } foreach (String key in var1.dicb.Keys) { MMBucket bucket = var1.dicb[key]; bucket.count = (bucket.count / dTotalMain1); foreach (String sub in bucket.dict.Keys) { bucket.dict[sub].count = (bucket.dict[sub].count / dTotalSub1); } } // var 2 if (var2 != null) { LoadGrid(); double dTotalMain2 = 0; double dTotalSub2 = 0; foreach (String key in var2.dicb.Keys) { MMBucket bucket = var2.dicb[key]; dTotalMain2 += bucket.count; foreach (String sub in bucket.dict.Keys) { dTotalSub2 += bucket.dict[sub].count; } } foreach (String key in var2.dicb.Keys) { MMBucket bucket = var2.dicb[key]; bucket.count = (bucket.count / dTotalMain2); foreach (String sub in bucket.dict.Keys) { bucket.dict[sub].count = (bucket.dict[sub].count / dTotalSub2); } } } // dispose if (bmp1 != null) { bmp1.Dispose(); } if (bmp2 != null) { bmp2.Dispose(); } if (mask != null) { mask.Dispose(); } }