2

I have read some posts and looked at a few different examples but I am still having a problem. My problem seems to lay with the location of the IFeature's shape, they are coming back as x= 590241.41279208846, y= 6284509.0955051426 (looks kind of like a UTM) but in order to see anything in that area they need to be more like x = -12831100.742200622; y = 7674669.7012844728.

This does not seem like a simple mouse point to map point location conversion.

This is my translation code:

    public void MoveVisibleContouringLayersToDrawingLayer()
    {
        IElement pElement = null;
        string polylineFeatureLayerBaseName = "Contour lines";
        IGraphicsContainer graphicsContainer = GetGraphicsContainer();
        graphicsContainer.DeleteAllElements();

        IEnumLayer layerEnumerator = MapController.Map.Layers;
        if (layerEnumerator != null)
        {
            try
            {
                ILayer layer = layerEnumerator.Next();
                while (null != layer)
                {
                    if (layer.Name == polylineFeatureLayerBaseName)
                    {
                        IFeatureLayer featureLayer = layer as IFeatureLayer;
                        IGeoFeatureLayer fl = (IGeoFeatureLayer)featureLayer;
                        if (null != featureLayer.FeatureClass)
                        {
                            if (featureLayer.Visible)
                            {
                                IFeatureCursor cursor = featureLayer.Search(null, false);
                                IFeature feature = cursor.NextFeature();
                                while (feature != null)
                                {
                                    // Noticing coordinates are no where near the coordinates I use to get
                                    // shapes to show up in my area. x = -12831100.742200622; y = 7674669.7012844728 
                                    // but the shape I am getting back is x= 590241.41279208846, y= 6284509.0955051426. Could the shape be in UTM?
                                    var a = feature.Shape.GeometryType.ToString();                                                      
                                    var b = feature.Shape.Envelope.UpperLeft.X;
                                    var c = feature.Shape.Envelope.UpperLeft.Y;
                                    var d = feature.Shape.Envelope.LowerRight.X;
                                    var e = feature.Shape.Envelope.LowerRight.Y;

                                    if (feature.Shape.GeometryType ==
                                        ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                                    {
                                        if (graphicsContainer != null)
                                        {
                                            IColor color = EsriConversionUtility.ConvertToRgbColor(Colors.Red);
                                            ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
                                            simpleLineSymbol.Color = color;
                                            simpleLineSymbol.Width = 3;

                                            // The IFeature shape it setting the lines geometry. 
                                            IGeometry geometry = feature.Shape;

                                            ILineElement lineElement = new LineElementClass();
                                            lineElement.Symbol = simpleLineSymbol;
                                            IElement elementToAdd = lineElement as IElement;
                                            elementToAdd.Geometry = geometry;
                                            graphicsContainer.AddElement(elementToAdd, 0);
                                        }
                                    }
                                        feature = cursor.NextFeature();
                                }
                            }
                        }                            
                    }

                    layer = layerEnumerator.Next();
                }
            }
            finally
            {
                Marshal.ReleaseComObject(layerEnumerator);
                RefreshView();                    
            }
        }
    }
  • I'm thinking that your data frame is in a different coordinate system to your feature class... Change the line IGeometry geometry = feature.Shape; to IGeometry geometry = feature.ShapeCopy; // don't use Shape, bad things can happen and then add a new line immediately after geometry.Project (ArcMap.Document.FocusMap.SpatialReference); // to project to the same CRS as the map. and see if that helps any. – Michael Stimson Sep 27 '18 at 22:43
  • That did it, thanks Michael. Add as an answer and I will accept it. – scott lafoy Oct 1 '18 at 13:18
3

It seems the problem is that the geometries are in a different coordinate system to the map. Although ArcMap will project on the fly that functionality is built in to the renderers (example IFeatureRenderer) and does not apply to geometries as graphics (example IElement). For graphic elements the spatial reference needs to match the map projection the graphic is being added to, most of the time this can be achieved by adding geometry.Project to your code; if there is a special transformation required between datums there's a little bit more to it than just calling Project.

if (graphicsContainer != null)
{
    IColor color = EsriConversionUtility.ConvertToRgbColor(Colors.Red);
    ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
    simpleLineSymbol.Color = color;
    simpleLineSymbol.Width = 3;

    // The IFeature shape it setting the lines geometry. 
    IGeometry geometry = feature.ShapeCopy; // don't use Shape, it can cause problems
    geometry.Project(ArcMap.Document.FocusMap.SpatialReference); // to project to the same CRS as the map

    ILineElement lineElement = new LineElementClass();
    lineElement.Symbol = simpleLineSymbol;
    IElement elementToAdd = lineElement as IElement;
    elementToAdd.Geometry = geometry;
    graphicsContainer.AddElement(elementToAdd, 0);
}

Please note the change to the line that obtains the geometry from the feature, use ShapeCopy in almost all situations as this will create a copy of the features' shape which isolates the geometry from the feature, if you get the shape directly you are playing with the real geometry associated with the feature and any modifications you make to the geometry object from then are made to the shape of the feature (including being assigned the next features' geometry) which can cause big problems.. read the comments on IFeature.Shape and IFeature.ShapeCopy which instructs the user to use ShapeCopy to get a copy of the geometry if they intend to modify the object in any way.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.