How to zoom to a selected point from xyEventSource file?

1598
7
03-15-2012 04:29 AM
LazarKovacevic
New Contributor
I made a code whitch work for features if i convert xyEventSource file to shape file but I need to zoom selected features if its in xyEventSource.
Im working in C#.

I fiiled checkedBox with rows of xyEventSource file but i cant zoom to checked item. if it s converted to a shape file i can zoom to checked item but i cant do that if its not converted to a shape file, and thats is the point. Can someone help me, pls?

p.s. Sorry for my bad english.
0 Kudos
7 Replies
BrianBottoms
New Contributor
Hi,
If you added an event table to ArcMap then right clicked over it and selected �??Display Route Events�?�, a layer should be added to ArcMap that behaves much the same way as other featureclasses. You should then have access to feature extents for zooming. If I understand your question, you populated a list with values from the xy event table. From there you would need to identify the layer created by doing the �??Display Route Events�?� then query that layer for the needed feature. I added code for a console app that shows one way to get to features built from an event source.
Brian

using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Location;
using ESRI.ArcGIS.Geometry;


namespace ZoomToEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Get license.
                ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
                //Open a map document with event tables and displayed event layers.
                MapDocumentClass pMapDocumentClass = new MapDocumentClass();
                string path = "C:\\temp\\Test.mxd";
                pMapDocumentClass.Open(path, string.Empty);
                IMapDocument pMapDocument = pMapDocumentClass;
                IMap pMap = pMapDocument.ActiveView.FocusMap;

                ILayer pLayer;
                IFeatureLayer2 pFl;
                IFeatureClass pFc;
                IDataLayer2 pDLayer;
                IRouteEventSourceName pESN;
                IDatasetName pDSN;
                IFeature pF;
                IEnvelope pEnv;
                IGeometry pGeo;
                IFeatureCursor pFcursor;

                Console.WriteLine("Checking " + path);
                Console.WriteLine();

                for (int i = 0; i < pMap.LayerCount; i++)//Loop TOC for displayed route events
                {
                   if (pMap.get_Layer(i) is IFeatureLayer)
                    {
                       pLayer = pMap.get_Layer(i);
                       pFl = (IFeatureLayer2)(pLayer);
                       pFc = pFl.FeatureClass;
                       if (pFc is IRouteEventSource)
                       {
                           //Identify source table of event data.
                           pDLayer = (IDataLayer2)pLayer;
                           pESN = (IRouteEventSourceName)pDLayer.DataSourceName;
                           pDSN = (IDatasetName)pESN.EventTableName;
                           Console.WriteLine("Found displayed event layer.");
                           Console.WriteLine("   Layer Name: " + pLayer.Name);
                           Console.WriteLine("   Source Table: " + pDSN.Name);
                           //Print first record's envelope extent - use for display extent...
                           pFcursor = pFc.Search(null, false);
                           pF = pFcursor.NextFeature();
                           pGeo = pF.Shape;
                           pEnv = pGeo.Envelope;
                           Console.WriteLine("   First record's extent: ");
                           Console.WriteLine("      " +pEnv.XMax + ", " + pEnv.YMax + ", " + pEnv.XMin + ", " + pEnv.YMin);
                           Console.WriteLine();
                       }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }

            Console.ReadLine();
        }
    }
       
}
0 Kudos
LazarKovacevic
New Contributor
I did something like your code and i got envelope with minx, miny, maxx, and maxy. Thats now works fine. But selection of specified point is not shown. Is there way to do that. This is code that i made:

private void timer1_Tick(object sender, EventArgs e)
        {
            ESRI.ArcGIS.Carto.IActiveView pAV;
            ESRI.ArcGIS.Carto.IMap pMap;
            ESRI.ArcGIS.Geodatabase.IEnumFeature pEnumF;
            ESRI.ArcGIS.Geodatabase.IFeature pF;
            IDocument doc2 = m_application.Document;
            IMxDocument mxdoc2 = (IMxDocument)doc2;
            IMap map2= mxdoc2.FocusMap;
            ITable table = (ITable)(map2.get_Layer(0) as IFeatureSelection);
            QueryFilter searchFilter;
            double minX, minY, maxX, maxY;
            minX = 45600048.45;
            maxX = 0;
            minY = 80000054.56;
            maxY = 0; 
            foreach (object cekirani in checkedListBox2.CheckedItems)
            {
                
                int d = cekirani.ToString().IndexOf(',');
                //MessageBox.Show(cekirani.ToString().Remove(d));
                searchFilter = new QueryFilter();
                searchFilter.WhereClause = "Ime='"+cekirani.ToString().Remove(d)+"'";
                ICursor kursor=(ICursor)table.Search(searchFilter,true);
                IRow row = kursor.NextRow();
                int ime=row.Fields.FindField("Ime");
                int alias = row.Fields.FindField("Alias");
                int iks = row.Fields.FindField("X");
                int ipsilon = row.Fields.FindField("Y");   
               
                while(row!=null)
                {
                    if (Convert.ToDouble(row.get_Value(iks))<minX)
                        minX = Convert.ToDouble(row.get_Value(iks));
                    if (Convert.ToDouble(row.get_Value(iks))>maxX)
                        maxX = Convert.ToDouble(row.get_Value(iks));
                    if (Convert.ToDouble(row.get_Value(ipsilon))<minY)
                        minY = Convert.ToDouble(row.get_Value(ipsilon));
                    if (Convert.ToDouble(row.get_Value(ipsilon))>maxY)
                        maxY = Convert.ToDouble(row.get_Value(ipsilon));
                    row = kursor.NextRow();
                }
            }   
            pMap = (IMap)map2;
            pEnumF = (IEnumFeature)pMap.FeatureSelection;
            pF = pEnumF.Next();
            ESRI.ArcGIS.Geometry.IEnvelope pEnv;
            pEnv = new EnvelopeClass();
            pEnv.SetEmpty(); 
            pEnv.XMin = minX;
            pEnv.XMax = maxX;
            pEnv.YMin = minY;
            pEnv.YMax = maxY;
            pEnv.Expand(2.5,2.5,true);
            pAV = mxdoc2.ActiveView;
            pAV.Extent = pEnv;
            pAV.Refresh();

        }
0 Kudos
BrianBottoms
New Contributor
Below is one way to display a feature as selected. I created a list box showing the OID values of the first feature layer (a feature layer from event table source). When a listbox item is clicked I add that OID to the selection set for the feature layer - I wrote it really quickly, but should give you some idea. Look up IFeatureSelection, ISelectionSet.

Incidentally, it might be worth noting that if you give the option of removing the event from the event table you need to setup ArcMap to display the removed event as it happens. SeeTechnical Article 25846...
Brian

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                IMap pMap = pMxDoc.FocusMap;
                //Clear all selected features.
                pMap.FeatureSelection.Clear();

                IFeatureLayer pFl = (IFeatureLayer)pMap.get_Layer(0);
                IFeatureClass pFc = pFl.FeatureClass;
                IQueryFilter pQf = new QueryFilterClass();
                pQf.WhereClause = pFc.OIDFieldName + " = " + listBox1.SelectedItem;
                IFeatureCursor pFcursor = pFc.Search(pQf, false);
                IFeature pF = pFcursor.NextFeature();
                IFeatureSelection pFsel = (IFeatureSelection)pFl;
                ISelectionSet pSel = pFsel.SelectionSet;
                if (pF != null)
                {
                    pSel.Add(Convert.ToInt32(listBox1.SelectedItem));
                    ZoomToFeature(pF);//method called to zoom to feature extent.
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught." + ex);
            }

        }
0 Kudos
LazarKovacevic
New Contributor
My xyLayer hasnt oidField, and i dont know how to set it:(
0 Kudos
JohnNelson3
New Contributor III
Code I use

           private void ZoomSel(double X, double Y)
           {
               try
               {
                   IMap iMap = iMxDoc.FocusMap;
                   IActiveView iAV = iMxDoc.ActiveView;
                   IPoint iPoint = new ESRI.ArcGIS.Geometry.Point();
                   IEnvelope Env = iAV.FullExtent;

                   iPoint.PutCoords(X,Y);
                   Env.CenterAt(iPoint);
                   iAV.Extent = (IEnvelope)Env;
                   iMap.MapScale = 2000;
                   iAV.Refresh();
               }
               catch
               {
                   MessageBox.Show("Unable to Zoom to Selected Location", "Zoom Error");
               }
           }
0 Kudos
PhilBlondin
New Contributor III
This all depends on what coordinate system the xy data is in and what coordinate system the map is in.
if you xy's are from a GPS unit then project a point to WGS84

            Dim ppoint As IPoint
            ppoint = New Point

            Dim pSpatialRefFactory As ISpatialReferenceFactory
            pSpatialRefFactory = New SpatialReferenceEnvironment
            Dim pWGS84CoordSys As IGeographicCoordinateSystem
            pWGS84CoordSys = pSpatialRefFactory.CreateGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_WGS1984)
          
            'X_,Y_ come from the workorder coordinates in database, pass to ppoint
            ppoint.X = X_
            ppoint.Y = Y_

            Dim pProjectedPoint As IPoint = ProjectGeometry(pWGS84CoordSys, ppoint, m_pMap)
          
            Dim pEnv As IEnvelope

            pEnv = m_pMxDoc.ActiveView.Extent
            pEnv.CenterAt(pProjectedPoint)
            m_pMxDoc.ActiveView.Extent = pEnv

            'you can expand the envelope from above by some factor since points don't have an envelope.
            'that is if you didn't want to use a predetermined scale like below

            m_pMap.MapScale = 1000 'set a scale so you are not zoomed in too tight

            m_pMxDoc.ActiveView.ScreenDisplay.Invalidate(Nothing, True, _
                   esriScreenCache.esriAllScreenCaches)
            System.Windows.Forms.Application.DoEvents()
0 Kudos
LazarKovacevic
New Contributor
private void timer1_Tick(object sender, EventArgs e)
{
ESRI.ArcGIS.Carto.IActiveView pAV;
ESRI.ArcGIS.Carto.IMap pMap;
ESRI.ArcGIS.Geodatabase.IEnumFeature pEnumF;
ESRI.ArcGIS.Geodatabase.IFeature pF;
IDocument doc2 = m_application.Document;
IMxDocument mxdoc2 = (IMxDocument)doc2;
IMap map2= mxdoc2.FocusMap;
ITable table = (ITable)(map2.get_Layer(0) as IFeatureSelection);
QueryFilter searchFilter;
double minX, minY, maxX, maxY;
minX = 45600048.45;
maxX = 0;
minY = 80000054.56;
maxY = 0;
foreach (object cekirani in checkedListBox2.CheckedItems)
{

int d = cekirani.ToString().IndexOf(',');
//MessageBox.Show(cekirani.ToString().Remove(d));
searchFilter = new QueryFilter();
searchFilter.WhereClause = "Ime='"+cekirani.ToString().Remove(d)+"'";
ICursor kursor=(ICursor)table.Search(searchFilter,true);
IRow row = kursor.NextRow();
int ime=row.Fields.FindField("Ime");
int alias = row.Fields.FindField("Alias");
int iks = row.Fields.FindField("X");
int ipsilon = row.Fields.FindField("Y");

while(row!=null)
{
if (Convert.ToDouble(row.get_Value(iks))<minX)
minX = Convert.ToDouble(row.get_Value(iks));
if (Convert.ToDouble(row.get_Value(iks))>maxX)
maxX = Convert.ToDouble(row.get_Value(iks));
if (Convert.ToDouble(row.get_Value(ipsilon))<minY)
minY = Convert.ToDouble(row.get_Value(ipsilon));
if (Convert.ToDouble(row.get_Value(ipsilon))>maxY)
maxY = Convert.ToDouble(row.get_Value(ipsilon));
row = kursor.NextRow();
}
}
pMap = (IMap)map2;
pEnumF = (IEnumFeature)pMap.FeatureSelection;
pF = pEnumF.Next();
ESRI.ArcGIS.Geometry.IEnvelope pEnv;
pEnv = new EnvelopeClass();
pEnv.SetEmpty();
pEnv.XMin = minX;
pEnv.XMax = maxX;
pEnv.YMin = minY;
pEnv.YMax = maxY;
pEnv.Expand(2.5,2.5,true);
pAV = mxdoc2.ActiveView;
pAV.Extent = pEnv;
pAV.Refresh();

} 


Like i said, this code above is doing what i want with zooming. I got my envelope with this, but the other points(coordinates) from layer(xyEventSource) which are near are shown and i want only point which make selection(according to searchFilter (llook at code)) to be different than others, highlighted or something like that(different color, size, style, whatever). Cause xyEventSource layer isnot selectable like shapefile. Can i make that points different(like change, symbol, unique renderer or something like that can work with my code above). Any suggestion please? I tried so many things but there must be something i just missed.
Thank tou!
0 Kudos