//-------------------------------------------------------------------------------------------------

        static void ParseResultXml(string ResultXml, List <Soil> Soils)
        {
            XmlDocument XMLdoc = new XmlDocument();

            try
            {
                XMLdoc.LoadXml(ResultXml);

                for (int FeatureIndex = 0; FeatureIndex < XMLdoc.GetElementsByTagName("gml:featureMember").Count; FeatureIndex++)
                {
                    XmlDocument XMLdocFeature = new XmlDocument();
                    XMLdocFeature.LoadXml(XMLdoc.GetElementsByTagName("gml:featureMember").Item(FeatureIndex).InnerXml);

                    for (int Index = 0; Index < XMLdocFeature.GetElementsByTagName("ms:mukey").Count; Index++)
                    {
                        // Skip surface water mapping units
                        if (XMLdocFeature.GetElementsByTagName("ms:musym").Item(Index).InnerText == "W")
                        {
                            continue;
                        }
                        Soils.Add(Ssurgo.AddSoil(XMLdocFeature, Index));
                    }
                }
                SortedList <String, Soil> UniqueLayerProperties = new SortedList <String, Soil>();
                foreach (Soil aSoil in Soils)
                {
                    Ssurgo.GetLayerProperties(aSoil, UniqueLayerProperties);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid response from SSURGO web service.");
                Ssurgo.FoundValidHSG = false;
            }
        }
//-------------------------------------------------------------------------------------------------

        public static List <Soil> FindSoils(double aLatitude, double aLongitude,
                                            double aDistance = 1000)
        {
            // Create a list of Soil objects to return
            FoundValidHSG = false;
            List <Soil> Soils = new List <Soil>();

            // Create an HTTP request to get all of the SSURGO mapping units
            // within Distance of the target location
            string Filter = "<Filter>"
                            + "  <DWithin>"
                            + "    <PropertyName>Geometry</PropertyName>"
                            + "    <gml:Point>"
                            + "       <gml:coordinates>"
                            + aLongitude.ToString()
                            + ","
                            + aLatitude.ToString()
                            + "</gml:coordinates>"
                            + "    </gml:Point>"
                            + "    <Distance%20units='m'>"
                            + aDistance.ToString()
                            + "</Distance>"
                            + "  </DWithin>"
                            + "</Filter>";



            string URL = "https://sdmdataaccess.nrcs.usda.gov/Spatial/SDMNAD83Geographic.wfs?Service=WFS&Version=1.0.0&Request=GetFeature&Typename=MapunitPoly&Filter="
                         + Filter;
            string ResultXml;

            // Create a WebClient object to submit the HTTP request
            WebClient client = new WebClient();

            try
            {
                // Submit the request and retrieve the response
                ResultXml = client.DownloadString(URL);

                // Parse the resulting XML with the mapping units info in it
                // to obtain boundaries and ultimately soil data for each unit
                Ssurgo.ParseResultXml(ResultXml, Soils);
            }
            catch
            {
                //MessageBox.Show(e.Message);
                throw;
            }
            client.Dispose();
            return(Soils);
        }
Exemple #3
0
        // Retrieve SSURGO soil data and polygons
        public static bool GetSoilData(double lat, double lng)
        {
            // Initialize soil data
            soilPolygons.Clear();
            List <Ssurgo.Soil> soils = null;

            // Establish a search radius around the site's lat/long
            double SoilsSearchRadius = SoilsRadiusIncrement;
            double SoilsMaxRadius    = 5 * SoilsRadiusIncrement;

            // Keep searching the USDA-NRCS's SSURGO DataMart web service within an expanding radius
            for (; ;)
            {
                try
                {
                    // Get soil data within current radius
                    soils = Ssurgo.FindSoils(lat, lng, SoilsSearchRadius);
                }
                catch
                {
                    MessageBox.Show("Could not access the SSURGO soils data base.\n" +
                                    "The service may be unavailable.", "Soils Data Search",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return(false);
                }

                // Data was found
                if (soils != null && soils.Count > 0)
                {
                    // Transfer polygons with soil data retrieved from web service to MapPolygon objects
                    int polyCount = 0;
                    for (int i = 0; i < soils.Count; i++)
                    {
                        polyCount += transferSoilPolygon(soils[i]);
                    }
                    if (polyCount > 0)
                    {
                        return(true);
                    }
                }

                // No data found -- ask user to increase the search radius
                if (SoilsSearchRadius < SoilsMaxRadius)
                {
                    string msg = "No soils data were found within a radius of " +
                                 SoilsSearchRadius + " meters. Expand the search?";
                    if (MessageBox.Show(msg, "Soils Data Search", MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    {
                        return(false);
                    }
                    SoilsSearchRadius += SoilsRadiusIncrement;
                }

                // Max. search radius reached
                else
                {
                    MessageBox.Show("No soils data were found for this site.", "Soils Data Search",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return(false);
                }
            }
        }