Ejemplo n.º 1
0
        /// <summary>
        /// We're about to render the page - search for the soils and write names to the
        /// response. The iPad soil app uses this method.
        /// </summary>
        protected void Page_PreRender(object sender, EventArgs e)
        {
            Response.Clear();
            Response.ContentType     = "text/plain";
            Response.ContentEncoding = System.Text.Encoding.UTF8;

            if (Request.QueryString["Name"] != null)
            {
                string SoilName = Request.QueryString["Name"];

                ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service();
                XmlDocument       Doc     = new XmlDocument();
                Doc.LoadXml(SoilsDB.SoilXML(SoilName));

                MemoryStream MemStream = new MemoryStream(10000);
                Doc.Save(MemStream);
                if (MemStream.ToArray() != null)
                {
                    Response.BinaryWrite(MemStream.ToArray());
                }
            }
            else if (Request.QueryString["NameJSON"] != null)
            {
                string SoilName = Request.QueryString["NameJSON"];

                ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service();
                Response.Write(SoilsDB.SoilAsJson(SoilName));
            }
            Response.End();
        }
Ejemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Name"] != null)
            {
                string FileName = Path.GetFileName(Request.QueryString["Name"]);

                Response.Clear();
                Response.AppendHeader("Content-Disposition", "inline; filename=" + FileName + ".csv");
                //Response.AppendHeader("Content-Disposition", "csv; Soils.csv");
                Response.Buffer      = false;
                Response.ContentType = "application/vnd.ms-excel"; // text/plain

                string SoilName = Request.QueryString["Name"];

                ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service();
                Soil Soil = Soil.Create(SoilsDB.SoilXML(SoilName));

                DataTable Data = new DataTable();
                SoilDataTable.SoilToTable(Soil, Data);

                Response.Write(DataTableUtility.DataTableToCSV(Data, 0));
                Response.Flush();                 // send our content to the client browser.
                Response.SuppressContent = true;  // stops .net from writing it's stuff.
            }
        }
        /// <summary>
        /// User has clicked on upload button.
        /// </summary>
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            using (ApsoilWeb.Service Soils = new Apsoil.ApsoilWeb.Service())
            {
                StreamReader In       = new StreamReader(File1.FileContent);
                string       contents = In.ReadToEnd();

                if (!userSoil)
                {
                    // Insert all soils into database.
                    Soils.UpdateAllSoils(contents);
                }
                else
                {
                    // Update a user soil.
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(contents);
                    foreach (var soil in XmlHelper.ChildNodes(doc.DocumentElement, "Soil"))
                    {
                        var soilParams = new ApsoilWeb.JsonSoilParam()
                        {
                            JSonSoil = JsonConvert.SerializeXmlNode(soil)
                        };
                        var ok = Soils.UpdateUserSoil(soilParams);
                    }
                }

                string[] AllSoils = Soils.SoilNames();
                SuccessLabel.Text    = "Success. " + AllSoils.Length.ToString() + " soils in database.";
                SuccessLabel.Visible = true;
            }
        }
Ejemplo n.º 4
0
        /// <summary>User has clicked show JSON button.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void ShowJSONClick(object sender, EventArgs e)
        {
            StringWriter writer = new StringWriter();

            writer.WriteLine("[");

            using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service())
            {
                List <string> selectedPaths = GetSelectedItems();
                foreach (string path in selectedPaths)
                {
                    writer.Write(soilsDB.SoilAsJson(path));
                    if (selectedPaths.Last() != path)
                    {
                        writer.WriteLine(",");
                    }
                    else
                    {
                        writer.WriteLine("");
                    }
                }
            }
            writer.WriteLine("]");
            ShowString(writer.ToString());
        }
Ejemplo n.º 5
0
        /// <summary>Handles the Load event of the Page control.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service())
            {
                List <string> allSoils = new List <string>();
                allSoils.AddRange(soilsDB.SoilNames());
                allSoils.Sort();

                if (Request.QueryString["Paths"] != null)
                {
                    allSoils.Clear();
                    string[] paths = Request.QueryString["Paths"].Split(";".ToCharArray());
                    allSoils.AddRange(paths);
                }

                List <string> selectedItems = GetSelectedItems();

                Label.Text = "Number of soils: " + allSoils.Count.ToString();
                ListBox.Items.Clear();
                foreach (string soilName in allSoils)
                {
                    ListItem item = new ListItem(soilName);
                    item.Selected = selectedItems.Contains(soilName);
                    ListBox.Items.Add(item);
                }
            }
        }
Ejemplo n.º 6
0
 private byte[] DrawChart()
 {
     if (Request.QueryString["Name"] != null)
     {
         // Google Earth passes in a soil name parameter.
         string            SoilName = Request.QueryString["Name"];
         ApsoilWeb.Service SoilsDB  = new Apsoil.ApsoilWeb.Service();
         return(SoilsDB.SoilChartPNG(SoilName));
     }
     else
     {
         // The iPad app passes in soil JSON rather than a name parameter.
         string json = FormBufferToString();
         if (json == null || json == string.Empty)
         {
             throw new Exception("No JSON specified in call to SoilChart.aspx");
         }
         XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
         if (doc == null)
         {
             throw new Exception("Invalid JSON passed to SoilChart.aspx");
         }
         ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service();
         byte[]            bytes   = SoilsDB.SoilChartPNGFromXML(doc.OuterXml);
         Context.Response.ContentType = "image/png";
         return(bytes);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// We're about to render the page - get the soil node and write it to the
        /// response.
        /// </summary>
        protected void Page_PreRender(object sender, EventArgs e)
        {
            Response.Clear();
            //Response.AppendHeader("Content-Disposition", "attachment; filename=Registrations.csv");
            Response.Buffer      = false;
            Response.ContentType = "text/plain";

            if (Request.QueryString["Latitude"] != null &&
                Request.QueryString["Longitude"] != null &&
                Request.QueryString["Radius"] != null)
            {
                double Latitude  = Convert.ToDouble(Request.QueryString["Latitude"]);
                double Longitude = Convert.ToDouble(Request.QueryString["Longitude"]);
                double Radius    = Convert.ToDouble(Request.QueryString["Radius"]);
                string asc       = Request.QueryString["asc"];

                ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service();

                // Make sure the soil search method works. The lat/long below is for soil:Black Vertosol-Anchorfield (Brookstead No006)
                string SoilNamesXML = SoilsDB.SearchSoils(Latitude, Longitude, Radius, asc);

                Response.Write(SoilNamesXML);
                Response.SuppressContent = true;  // stops .net from writing it's stuff.
            }
        }
Ejemplo n.º 8
0
 /// <summary>Delete selected soils.</summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void OnDeleteClick(object sender, EventArgs e)
 {
     using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service())
     {
         foreach (ListItem item in ListBox.Items)
         {
             if (item.Selected)
             {
                 string soilPath = item.Text;
                 soilsDB.Delete(soilPath);
             }
         }
     }
     Response.Redirect("Apsoil.aspx");
 }
Ejemplo n.º 9
0
        /// <summary>Do a search for soils and display them.</summary>
        private void DoSearch(bool withPAWC)
        {
            using (ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service())
            {
                List <double> thickness = new List <double>();
                List <double> PAWC      = new List <double>();
                List <double> grav      = new List <double>();

                GetThicknessAndPAWC(Thickness1, PAWC1, Grav1, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness2, PAWC2, Grav2, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness3, PAWC3, Grav3, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness4, PAWC4, Grav4, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness5, PAWC5, Grav5, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness6, PAWC6, Grav6, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness7, PAWC7, Grav7, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness8, PAWC8, Grav8, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness9, PAWC9, Grav9, ref thickness, ref PAWC, ref grav);
                GetThicknessAndPAWC(Thickness10, PAWC10, Grav10, ref thickness, ref PAWC, ref grav);

                string[] closestSoils;
                if (withPAWC)
                {
                    closestSoils = SoilsDB.ClosestMatchingSoils(thickness.ToArray(), PAWC.ToArray(), grav.ToArray(), "Wheat", 10, CheckBox1.Checked, true);
                }
                else
                {
                    closestSoils = SoilsDB.ClosestMatchingSoils(thickness.ToArray(), null, grav.ToArray(), "Wheat", 10, CheckBox1.Checked, true);
                }

                string soilNames = string.Empty;
                foreach (string path in closestSoils)
                {
                    if (soilNames != string.Empty)
                    {
                        soilNames += ";";
                    }

                    soilNames += path;
                }

                Response.Redirect("Apsoil.aspx?Paths=" + soilNames);
            }
        }
Ejemplo n.º 10
0
        /// <summary>User has clicked on show XML button.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void ShowXMLClick(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();

            doc.AppendChild(doc.CreateElement("folder"));
            XmlHelper.SetAttribute(doc.DocumentElement, "name", "Soils");
            using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service())
            {
                foreach (string path in GetSelectedItems())
                {
                    XmlDocument localXml = new XmlDocument();
                    localXml.LoadXml(soilsDB.SoilXML(path));
                    doc.DocumentElement.AppendChild(doc.ImportNode(localXml.DocumentElement, true));
                }
            }
            StringWriter writer = new StringWriter();

            writer.Write(XmlHelper.FormattedXML(doc.DocumentElement.OuterXml));
            ShowString(writer.ToString());
        }
Ejemplo n.º 11
0
        /// <summary>Check selected soils and show error messages.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnCheckSoilsClick(object sender, EventArgs e)
        {
            string labelText = string.Empty;

            using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service())
            {
                foreach (ListItem item in ListBox.Items)
                {
                    if (item.Selected)
                    {
                        string soilPath = item.Text;
                        Soil   soil     = Soil.Create(soilsDB.SoilXML(soilPath));
                        string messages = soil.Check(true);
                        if (messages != string.Empty)
                        {
                            labelText += soilPath + "\r\n";
                            labelText += messages + "\r\n";
                        }
                    }
                }
            }
            label3.Text = labelText.Replace("\r\n", "<br/>");
        }
Ejemplo n.º 12
0
        /// <summary>
        /// User has clicked on upload button.
        /// </summary>
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            using (ApsoilWeb.Service Soils = new Apsoil.ApsoilWeb.Service())
            {
                StreamReader In       = new StreamReader(File1.FileContent);
                string       contents = In.ReadToEnd();

                if (pathToOverride == null)
                {
                    // Insert all soils into database.
                    Soils.UpdateAllSoils(contents);
                }
                else
                {
                    // Update a single soil.
                    Soils.UpdateSoil(pathToOverride, contents);
                }

                string[] AllSoils = Soils.SoilNames();
                SuccessLabel.Text    = "Success. " + AllSoils.Length.ToString() + " soils in database.";
                SuccessLabel.Visible = true;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Test method for creating a chart from JSON.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void OnSoilChartFromJson(object sender, EventArgs e)
        {
            string SelectedName = ListBox.SelectedValue;

            using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service())
            {
                string json = soilsDB.SoilAsJson(SelectedName);

                byte[] bytes = Encoding.Default.GetBytes(json);

                HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("https://apsimdev.apsim.info/ApsoilWeb/SoilChart.aspx");
                //Our method is post, otherwise the buffer (postvars) would be useless
                WebReq.Method = "POST";
                //We use form contentType, for the postvars.
                WebReq.ContentType = "text/plain";
                //The length of the buffer (postvars) is used as contentlength.
                WebReq.ContentLength = bytes.Length;
                //We open a stream for writing the postvars
                Stream PostData = WebReq.GetRequestStream();
                //Now we write, and afterwards, we close. Closing is always important!
                PostData.Write(bytes, 0, bytes.Length);
                PostData.Close();

                HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

                Stream Answer     = WebResp.GetResponseStream();
                byte[] chartBytes = new byte[200000];
                int    numBytes   = Answer.Read(chartBytes, 0, chartBytes.Length);
                Array.Resize(ref chartBytes, numBytes);

                Response.Clear();
                Response.ContentType = "image/png";
                Response.BinaryWrite(chartBytes);
                Response.Flush();
                Response.End();
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// This is called directly by Google Earth when it wants KML data.
        /// </summary>
        private byte[] NetworkLinkData()
        {
            kml      KmlContent = new kml();
            Document KmlDoc     = new Document("Test", "");

            KmlContent.Documents.Add(KmlDoc);

            KML_22_Beta1.Style APSRUIcon = new KML_22_Beta1.Style();
            APSRUIcon.IconStyle.Icon.Href.Text = "http://apsimdev.apsim.info/ApsoilWeb/shovel.png";
            APSRUIcon.IconStyle.Scale.Value    = 0.7;
            APSRUIcon.LabelStyle.Scale.Value   = 0.7;
            APSRUIcon.id = "APSRUIconID";
            KmlDoc.StyleSelector.Style.Add(APSRUIcon);

            Dictionary <string, Folder> Folders = new Dictionary <string, Folder>();

            ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service();
            foreach (string Name in SoilsDB.AllSoilNames(false))
            {
                string FolderName = Name;
                if (FolderName.Contains("/"))
                {
                    FolderName = FolderName.Substring(0, FolderName.LastIndexOf('/'));
                }

                Soil Soil = Soil.Create(SoilsDB.SoilXML(Name));
                if (Soil != null)
                {
                    string BalloonDescription = "<p><b>" + Soil.Name + "</b></p><p><i>"
                                                + Soil.Comments + "</i></p>";

                    string DataSourceComments = Soil.DataSource;
                    if (DataSourceComments != null && DataSourceComments != "")
                    {
                        BalloonDescription += "<p><i>Data source: " + DataSourceComments + "</i></p>";
                    }

                    BalloonDescription += "<img src=\"" + ourPath + "SoilChart.aspx?Name=" + Name + "\"/>";

                    BalloonDescription += "<p><a href=\"" + ourPath + "GetSoil.aspx?Name=" + Name + "\">Download soil in APSIM format (copy and paste contents to your simulation).</a></p>";
                    BalloonDescription += "<p><a name=\"link_id\" id=\"link_id\"  href=\"Download.html\" onclick=\"window.open('" + ourPath + "Excel.aspx?Name=" + Name + "');\">Download soil as an EXCEL spreadsheet</a></p>";

                    string SoilName = Soil.Name;
                    if (Soil.ApsoilNumber != "")
                    {
                        SoilName = Soil.ApsoilNumber;
                    }
                    Placemark plmMyPlaceMark = new Placemark(SoilName,
                                                             BalloonDescription,
                                                             Soil.Latitude,
                                                             Soil.Longitude,
                                                             0, altitudeModeEnum.clampToGround);
                    plmMyPlaceMark.Description.UseCDATA = true;
                    plmMyPlaceMark.Description.Text     = BalloonDescription;
                    plmMyPlaceMark.StyleUrl.Text        = "#APSRUIconID";

                    Folder F = GetFolder(FolderName, Folders, KmlDoc);
                    F.Features.Placemarks.Add(plmMyPlaceMark);
                }
            }

            KmlContent.NetworkLinkControl.Expires.Value        = DateTime.Now.AddDays(7);
            KmlContent.NetworkLinkControl.LinkDescription.Text = "Characterised sites - " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss");
            return(KmlContent.GetKMZ("NetworkData.kml"));
        }