Class from Productivity Suite that does BNG Transformation and Grid Lookup
        public void TestGetExtentsHandlesIncorrectValues(string gridRef)
        {
            BindRunTime();

            TranslateGridReference translate = new TranslateGridReference();
            Envelope envelope = translate.GetExtents(gridRef);
            Assert.IsTrue(envelope == null);
        }
        public void TestGetExtentsReturnsCorrectValues(string gridRef, double expectedX, double expectedY)
        {
            BindRunTime();

            TranslateGridReference translate = new TranslateGridReference();
            Envelope envelope = translate.GetExtents(gridRef);
            Assert.IsTrue(envelope.XMin == expectedX);
            Assert.IsTrue(envelope.YMin == expectedY);
        }
Example #3
0
        /// <summary>
        /// </summary>
        /// <param name="queryString">The string to search for, either XXXXXX,YYYYY or with sheet SSXXXXYYYY</param>
        /// <returns>An ArcObjects envelope object</returns>
        private Envelope DoMatchLookup(string queryString)
        {
            _log.Debug("BNGLocator DoMatchLookup");
            Envelope envelope = null;
            string matchText;
            string[] parts;

            // Removes '," and spaces and split into parts based on a "," delimiter
            matchText = queryString.Replace("'", "");
            matchText = matchText.Replace("\"", "");
            matchText = matchText.TrimStart(' ').TrimEnd(' ');

            // Split the search string
            parts = matchText.Split(',');

            if (parts.Length == 1)
            {
                parts = matchText.Split(' ');
            }

            // If there are two parts then it's a co-ordinate X,Y so we construct an
            // envelope and return it. If there is one part then we assume it's a
            // tile reference e.g. TQ1234 so we call TranslateGridReference to get
            // the OS envelope.

            if (parts.Length == 2)
            {
                double xCoordinate;
                double yCoordinate;

                if ((double.TryParse(parts[0], out xCoordinate) == true) & (double.TryParse(parts[1], out yCoordinate) == true))
                {
                    try
                    {
                        envelope = new EnvelopeClass();
                        envelope.XMin = xCoordinate;
                        envelope.YMin = yCoordinate;
                        envelope.XMax = xCoordinate;
                        envelope.YMax = yCoordinate;
                    }
                    catch (Exception)
                    {
                        envelope = null;
                    }
                }
            }
            else
            {
                TranslateGridReference translate = new TranslateGridReference();
                matchText = matchText.Replace(" ", string.Empty);
                envelope = translate.GetExtents(matchText);
            }

            return envelope;
        }
Example #4
0
        /// <summary>
        /// Generate an address based on a point.
        /// </summary>
        /// <param name="location"></param>
        /// <param name="returnIntersection"></param>
        /// <returns></returns>
        public override IPropertySet ReverseGeocode(IPoint location, bool returnIntersection)
        {
            _log.Debug("IReverseGeocode ReverseGeocode");
            string matchText;
            IPropertySet reverseGeocodedResult = new PropertySetClass();
            Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
            System.Object obj = Activator.CreateInstance(factoryType);
            var srf = obj as ISpatialReferenceFactory3;
            var wgs84GCS = srf.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

            // Sometimes LatLong is incorrectly identified as BNG so it must be checked
            if ((location.X > -9 && location.X < 2) && (location.Y > 49 && location.Y < 61))
            {
                location.SpatialReference = wgs84GCS;
            }

            // Only supports coords in WGS 84 or BNG
            if (location.SpatialReference.Name == "GCS_WGS_1984")
            {
                #region Project coordinates to BNG accurately

                // Create Transformation from WGS84 to OSGB86
                var geoTrans = srf.CreateGeoTransformation((int)esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984Petrol) as IGeoTransformation;

                ISpatialReference fromSpatialReference;
                ISpatialReference toSpatialReference;
                geoTrans.GetSpatialReferences(out fromSpatialReference, out toSpatialReference);

                // Use correct coord systems to ensure accuracy
                var bngPCS = srf.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_BritishNationalGrid);
                if ((wgs84GCS.FactoryCode != toSpatialReference.FactoryCode)
                    || (bngPCS.GeographicCoordinateSystem.FactoryCode != fromSpatialReference.FactoryCode))
                {
                    throw new Exception("invalid geotransformation");
                }

                IGeometry5 geometry;
                geometry = location as IGeometry5;
                geometry.SpatialReference = wgs84GCS;

                geometry.ProjectEx(bngPCS, esriTransformDirection.esriTransformReverse, geoTrans, false, 0.0, 0.0);
                location = geometry as IPoint;

                #endregion
            }
            else if (location.SpatialReference.Name != "British_National_Grid")
            {
                // Unaccepted spatial reference, do not process
                return reverseGeocodedResult;
            }

            TranslateGridReference translate = new TranslateGridReference();
            // Translate the BNG coords to a British Grid Reference
            matchText = translate.GetGridReference(location);
            object names = null;
            object values = null;

            // The values being returned must include a geometry, any extra info fields ("X", "Y", "Addr_type") and at least
            // one field with the same name as an input field, "BNG" in this case, to hold the result fields, mathcText in this case
            names = new string[] { "Shape", "X Field", "Y Field", "BNG" , "Addr_type", "Match_addr" };
            values = new object[] { location, location.X, location.Y, matchText.ToString(), "BNG", matchText.ToString() };

            reverseGeocodedResult.SetProperties(names, values);

            return reverseGeocodedResult;
        }