public List<City> SearchCities(string userName, string password, double centerX, double centerY, double distance) { IWorkspace workspace = null; IFeatureClass citiesClass = null; IFeatureCursor citiesCursor = null; try { workspace = Connect(userName, password); //IFeatureClass citiesClass = (workspace as IFeatureWorkspace).OpenFeatureClass("SDE.SDE.Cities"); citiesClass = (workspace as IFeatureWorkspace).OpenFeatureClass("Cities"); int areaNameIndex = citiesClass.FindField("AREANAME"); IPoint center = new Point(); center.PutCoords(centerX, centerY); IGeometry buffer = (center as ITopologicalOperator).Buffer(distance); ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = buffer; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureCursor citiesCursor = citiesClass.Search(spatialFilter, true); List<City> resultCities = new List<City>(); IFeature capital = citiesCursor.NextFeature(); IPoint capitalShape = null; while (capital != null) { capitalShape = capital.Shape as IPoint; resultCities.Add(new City() { AreaName = capital.get_Value(areaNameIndex) as string, X = capitalShape.X, Y = capitalShape.Y }); capital = citiesCursor.NextFeature(); } return resultCities; } finally { if (citiesClass != null) { Marshal.ReleaseComObject(citiesClass); } } }
private static IPoint getCenter(double centerX, double centerY) { IPoint center = new Point(); center.PutCoords(centerX, centerY); return center; }
/// <summary> /// Method used to convert a string to a known coordinate /// Assumes WGS84 for now /// Uses the IConversionNotation interface /// </summary> /// <param name="coordinate">the coordinate as a string</param> /// <returns>IPoint if successful, null if not</returns> internal IPoint GetPointFromString(string coordinate) { Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment"); System.Object obj = Activator.CreateInstance(t); ISpatialReferenceFactory srFact = obj as ISpatialReferenceFactory; // Use the enumeration to create an instance of the predefined object. IGeographicCoordinateSystem geographicCS = srFact.CreateGeographicCoordinateSystem((int) esriSRGeoCSType.esriSRGeoCS_WGS1984); var point = new Point() as IPoint; point.SpatialReference = geographicCS; var cn = point as IConversionNotation; if (cn == null) return null; try { cn.PutCoordsFromDD(coordinate); return point; } catch { } try { cn.PutCoordsFromDDM(coordinate); return point; } catch { } try { cn.PutCoordsFromDMS(coordinate); return point; } catch { } try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeCENTER, coordinate); return point; } catch { } try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeLL, coordinate); return point; } catch { } try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_Automatic); return point; } catch { } try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_NewStyle); return point; } catch { } try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_NewWith180InZone01); return point; } catch { } try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_OldStyle); return point; } catch { } try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_OldWith180InZone01); return point; } catch { } try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_USNG); return point; } catch { } try { cn.PutCoordsFromUSNG(coordinate); return point; } catch { } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMAddSpaces, coordinate); return point; } catch { } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMUseNS, coordinate); return point; } catch { } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMAddSpaces|esriUTMConversionOptionsEnum.esriUTMUseNS, coordinate); return point; } catch { } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMNoOptions, coordinate); return point; } catch { } try { cn.PutCoordsFromGeoRef(coordinate); return point; } catch { } // lets see if we have a PCS coordinate // we'll assume the same units as the map units // get spatial reference of map if (ArcMap.Document == null || ArcMap.Document.FocusMap == null || ArcMap.Document.FocusMap.SpatialReference == null) return null; var map = ArcMap.Document.FocusMap; var pcs = map.SpatialReference as IProjectedCoordinateSystem; if (pcs == null) return null; point.SpatialReference = map.SpatialReference; // get pcs coordinate from input coordinate = coordinate.Trim(); Regex regexMercator = new Regex(@"^(?<latitude>\-?\d+\.?\d*)[+,;:\s]*(?<longitude>\-?\d+\.?\d*)"); var matchMercator = regexMercator.Match(coordinate); if (matchMercator.Success && matchMercator.Length == coordinate.Length) { try { var Lat = Double.Parse(matchMercator.Groups["latitude"].Value); var Lon = Double.Parse(matchMercator.Groups["longitude"].Value); point.PutCoords(Lon, Lat); return point; } catch { return null; } } return null; }
private static bool InsertFeatures(fulcrumrecords fulcrumRecords, IFeatureClass featureClass) { try { // Create an insert cursor. IFeatureCursor insertCursor = featureClass.Insert(false); //comReleaser.ManageLifetime(insertCursor); foreach (fulcrumrecord record in fulcrumRecords.records) { // Create a feature buffer. IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer(); //comReleaser.ManageLifetime(featureBuffer); IPoint pt = new Point(); pt.PutCoords(record.longitude, record.latitude); featureBuffer.Shape = pt as IGeometry; //handle photos separately if (record.photos.Count >= 1) { try { //only link to the first photo for now //ToDo: how to put the actual image in the photos field (the actual image is held in the photos property of the record) //like so... //featureBuffer.set_Value(featureClass.FindField("photos"), record.photos[0]); featureBuffer.set_Value(featureClass.FindField(photoFieldName), record.photolinks[0]); //ToDo: enhancement: multiple photo support; could create a separate table to store the photo links and join it back to resulting feature class via record id } catch (Exception e) { string photoInsertError = e.Message; } } foreach (fulcrumattribute attr in record.attributes) { if (attr.fieldName != "photos") { //ToDo: why are some (first?) field names empty strings? if (attr.fieldName != "" && attr.fieldName != null) { try { featureBuffer.set_Value(featureClass.FindField(attr.fieldName), attr.fieldValue); } catch(Exception e) { string ouch = e.Message; } } } } try { insertCursor.InsertFeature(featureBuffer); featureBuffer = null; GC.Collect(); } catch (Exception e) { string insertFailed = e.Message; } } // Flush the buffer to the geodatabase. insertCursor.Flush(); return true; } catch (Exception) { return false; } }
private IPoint LowerLeftPoint(int row, int column) { IPoint point = new Point(); double x = Extents.XMin + column * ColumnWidth; double y = Extents.YMin + row * RowHeight; point.PutCoords(x, y); return point; }
protected override void OnClick() { // Create two points. IPoint fromPoint = new Point(); fromPoint.PutCoords(1300757, 554219); IPoint toPoint = new Point(); toPoint.PutCoords(1300759, 554217); // Note: Spatial Reference = NAD_1983_StatePlane_Washington_North_FIPS_4601_Feet // ****** No Need to go through IPointCollection **** //// Add the points to a point collection //object missing = Type.Missing; //IPointCollection pointCollection = new Multipoint(); //pointCollection.AddPoint(fromPoint, ref missing, ref missing); //pointCollection.AddPoint(toPoint, ref missing, ref missing); // ****** No Need to go through IPointCollection **** // Get a reference to a feature class from ArcMap IMap map = ArcMap.Document.ActiveView as IMap; ILayer layer = map.get_Layer(0); IFeatureLayer featureLayer = layer as FeatureLayer; IFeatureClass featureClass = featureLayer.FeatureClass; IFeature feature = featureClass.CreateFeature(); IPolyline polyline = new PolylineClass(); polyline.FromPoint = fromPoint; polyline.ToPoint = toPoint; //IPolyline polyline = pointCollection as IPolyline; // This leads to a null polyline feature.Shape = polyline as IGeometry; feature.Store(); // ---- Symbolize the polyline with the ArrorMarker at both ends of the line ----------- // See: http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#//0001000008w8000000 ICartographicLineSymbol ipArrowLineSymbol = new CartographicLineSymbol(); // the line color will be red IRgbColor ipRgbRedColor = new RgbColorClass(); ipRgbRedColor.Red = 192; // the arrow will be black IRgbColor ipRgbBlackColor = new RgbColorClass(); ipRgbBlackColor.RGB = 0; // set up the arrow that will be displayed along the line IArrowMarkerSymbol ipArrowMarker = new ArrowMarkerSymbolClass(); ipArrowMarker.Style = esriArrowMarkerStyle.esriAMSPlain; ipArrowMarker.Length = 18; ipArrowMarker.Width = 12; ipArrowMarker.Color = ipRgbBlackColor; // set up the line itself ipArrowLineSymbol.Width = 4; ipArrowLineSymbol.Color = ipRgbRedColor; // decorate the line with the arrow symbol ISimpleLineDecorationElement ipSimpleLineDecorationElement = new SimpleLineDecorationElementClass(); ipSimpleLineDecorationElement.Rotate = true; ipSimpleLineDecorationElement.PositionAsRatio = true; ipSimpleLineDecorationElement.MarkerSymbol = ipArrowMarker; ipSimpleLineDecorationElement.AddPosition(0.0); ipSimpleLineDecorationElement.AddPosition(1.0); ipSimpleLineDecorationElement.FlipFirst = true; ILineDecoration ipLineDecoration = new LineDecorationClass(); ipLineDecoration.AddElement(ipSimpleLineDecorationElement); ((ILineProperties)ipArrowLineSymbol).LineDecoration = ipLineDecoration; // Create renderer and apply it to the layer ISimpleRenderer simpleRenderer = new SimpleRendererClass(); simpleRenderer.Symbol = ipArrowLineSymbol as ISymbol; IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer; geoFeatureLayer.Renderer = simpleRenderer as IFeatureRenderer; IMxDocument mxDocument = ArcMap.Application.Document as IMxDocument; mxDocument.ActiveView.Refresh(); mxDocument.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, geoFeatureLayer, mxDocument.ActiveView.Extent); mxDocument.UpdateContents(); // ---- Symbolize the polyline with the ArrorMarker at both ends of the line ----------- ArcMap.Application.CurrentTool = null; }
private void drawLineGraphic(double Size, bool bHor = true) { IMxDocument pMxDoc = m_pDoc; IActiveView pActiveView; IScreenDisplay pScreenDisplay; IEnvelope pEnv; IPoint pCenterPoint; IDisplayTransformation pTransform; pActiveView = pMxDoc.FocusMap as IActiveView; pScreenDisplay = pActiveView.ScreenDisplay; pTransform = pScreenDisplay.DisplayTransformation; pEnv = pTransform.FittedBounds; pCenterPoint = new Point(); pCenterPoint.PutCoords((pEnv.XMax + pEnv.XMin) / 2, (pEnv.YMax + pEnv.YMin) / 2); ILine pLine; IPolyline pPolyline; IGeometryCollection pGeomColl; ISegmentCollection pSegColl; Size /= 2; IPoint pPointFrom; IPoint pPointTo; pSegColl = new Path() as ISegmentCollection; pGeomColl = new Polyline() as IGeometryCollection; // UN pPointFrom = new Point(); if (bHor) { pPointFrom.PutCoords(pCenterPoint.X - Size, pCenterPoint.Y); } else { pPointFrom.PutCoords(pCenterPoint.X, pCenterPoint.Y - Size); } pPointTo = new Point(); if (bHor) { pPointTo.PutCoords(pCenterPoint.X + Size, pCenterPoint.Y); } else { pPointTo.PutCoords(pCenterPoint.X, pCenterPoint.Y + Size); } pLine = new Line(); pLine.PutCoords(pPointFrom, pPointTo); pSegColl.AddSegment(pLine as ISegment); // Add to final pGeomColl.AddGeometry(pSegColl as IGeometry); pPolyline = pGeomColl as IPolyline; IRgbColor pLineColor = new RgbColor(); pLineColor.Red = 255; // A CHANGER!!! addGraphicToMap(pMxDoc.FocusMap, pPolyline, pLineColor, pLineColor); pPointFrom = null; pPointTo = null; pLine = null; pPolyline = null; pGeomColl = null; pSegColl = null; }
private void drawCircleGraphic(double Size) { IMxDocument pMxDoc = m_pDoc; IActiveView pActiveView; IScreenDisplay pScreenDisplay; IEnvelope pEnv; IPoint pCenterPoint; IDisplayTransformation pTransform; pActiveView = pMxDoc.FocusMap as IActiveView; pScreenDisplay = pActiveView.ScreenDisplay; pTransform = pScreenDisplay.DisplayTransformation; pEnv = pTransform.FittedBounds; pCenterPoint = new Point(); pCenterPoint.PutCoords((pEnv.XMax + pEnv.XMin) / 2, (pEnv.YMax + pEnv.YMin) / 2); IConstructCircularArc pCircle = new CircularArc() as IConstructCircularArc; ISegmentCollection pPolygon = new Polygon() as ISegmentCollection; Size *= 10000; Size = Math.Sqrt(Size / 3.1416); pCircle.ConstructCircle(pCenterPoint, Size, true); pPolygon.AddSegment(pCircle as ISegment); IPolygon pFinalPolygon; pFinalPolygon = pPolygon as IPolygon; pFinalPolygon.Close(); IRgbColor pLineColor = new RgbColor(); pLineColor.Red = 255; // A CHANGER!!! addGraphicToMap(pMxDoc.FocusMap, pFinalPolygon, pLineColor, pLineColor); }
private IPoint[] GetPoints(string sInput, ISpatialReference pSR) { int iStrCnt; int iPairCnt; string[] sNumbers; IPoint[] pPts; IPoint pPt; int i; int iPos; double dX; double dY; sNumbers = sInput.Split(','); iStrCnt = sNumbers.GetUpperBound(0) + 1; iPairCnt = iStrCnt / 2; Logger.WriteLine("Pairs:" + iPairCnt + "," + iStrCnt); if ((iPairCnt * 2) == iStrCnt) { pPts = new ESRI.ArcGIS.Geometry.IPoint[iPairCnt]; iPos = 0; for (i = 0; i < iPairCnt; i++) { pPt = new Point(); pPt.SpatialReference = pSR; dX = Convert.ToDouble(sNumbers[iPos]); dY = Convert.ToDouble(sNumbers[iPos + 1]); Logger.WriteLine("(" + i + "DX,DY:" + dX + "," + dY); pPt.PutCoords(dX, dY); pPts[i] = pPt; iPos = iPos + 2; } return pPts; } else { return null; } }
private bool CREATEPOINTFEATURE(string sClassName, string sPoints, string sVars) { IFeatureClass pFC; IFeatureWorkspace pFWKS; IFeature pFeat; IPoint pPt; IMxDocument pMXDoc; string[,] sVar; double dX; double dY; string[] sPts; try { SW1.Reset(); SW1.Start(); pMXDoc = GetDoc(); sVar = GetVars(sVars); sPts = sPoints.Split(','); pPt = new Point(); pPt.SpatialReference = pMXDoc.FocusMap.SpatialReference; dX = Convert.ToDouble(sPts[0]); dY = Convert.ToDouble(sPts[1]); pPt.PutCoords(dX, dY); if (pPt != null) { pFWKS = (IFeatureWorkspace)Workspace; pFC = pFWKS.OpenFeatureClass(sClassName); if (pFC != null) { if (pEditor != null) { pEditor.StartOperation(); pFeat = pFC.CreateFeature(); pFeat.Shape = (IGeometry)pPt; SetFields(pFeat, sVar); pFeat.Store(); pEditor.StopOperation("Create Point"); } else { Logger.WriteLine("Editor not found"); } } SW1.Stop(); RecordActionTime("CreatePointFeature: ", SW1.ElapsedMilliseconds); return true; } else { Logger.WriteLine("Null Geometry"); return false; } } catch (Exception EX) { Logger.WriteLine("Error in CreatePointFeature: " + EX.Message + ":" + EX.StackTrace); return false; } }