GisDevelop_Exp/GisDevelop_Exp/MainWindow.xaml.cs
2024-10-12 11:42:36 +08:00

248 lines
10 KiB
C#

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Color = System.Drawing.Color;
using Polygon = Esri.ArcGISRuntime.Geometry.Polygon;
namespace GisDevelop_Exp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private FeatureLayer _featureLayer;
private string _statesUrl =
"https://services.arcgis.com/jIL9msH9OI208GCb/arcgis/rest/services/USA_Daytime_Population_2016/FeatureServer/0";
private ServiceFeatureTable _featureTable;
private readonly Dictionary<string, Basemap> _basemapOptions = new Dictionary<string, Basemap>()
{
{ "Streets (Raster)", new Basemap(BasemapStyle.OSMStreets) },
{ "Streets (Vector)", new Basemap(BasemapStyle.OSMStreetsRelief) },
{ "Streets - Night (Vector)", new Basemap(BasemapStyle.ArcGISStreetsNight) },
{ "Imagery (Raster)", new Basemap(BasemapStyle.ArcGISImagery) },
{ "Imagery with Labels (Raster)", new Basemap(BasemapStyle.ArcGISImageryLabels) },
{ "Imagery with Labels (Vector)", new Basemap(BasemapStyle.ArcGISImageryStandard) },
{ "Dark Gray Canvas (Vector)", new Basemap(BasemapStyle.ArcGISDarkGray) },
{ "Light Gray Canvas (Raster)", new Basemap(BasemapStyle.ArcGISLightGray) },
{ "Light Gray Canvas (Vector)", new Basemap(BasemapStyle.ArcGISLightGrayBase) },
{ "Navigation (Vector)", new Basemap(BasemapStyle.ArcGISNavigation) },
{ "OpenStreetMap (Raster)", new Basemap(BasemapStyle.OSMNavigation) }
};
public MainWindow()
{
InitializeComponent();
Initialize();
EagleMapView.Map = new Map(BasemapStyle.ArcGISImageryStandard);
}
private void Initialize()
{
MainMapView.Map = new Map(_basemapOptions.Values.First());
BasemapChooser.ItemsSource = _basemapOptions.Keys;
BasemapChooser.SelectedIndex = 0;
}
private void Menu_OpenShp_Click(object sender, RoutedEventArgs e)
{
OpenShp();
}
private async void OpenShp()
{
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Filter = "Shapefile文件(*.shp)|*.shp";
fileDialog.Title = "打开Shapefile文件";
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
for (int i = 0; i < fileDialog.FileNames.Length; i++)
{
String filePath = fileDialog.FileNames.GetValue(i).ToString();
ShapefileFeatureTable openedFeature = await ShapefileFeatureTable.OpenAsync(filePath);
FeatureLayer opendFeatureLayer = new FeatureLayer(openedFeature);
MainMapView.Map.OperationalLayers.Add(opendFeatureLayer);
await MainMapView.SetViewpointGeometryAsync(opendFeatureLayer.FullExtent);
}
}
}
private async void OpenGeoDatabase()
{
string _slocalGeodatabasePath = "";
Geodatabase _localGeodatabase;
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Filter = "Geodatabase文件(*.geodatabase)|*.geodatabase";
if(fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
_slocalGeodatabasePath = fileDialog.FileName;
}
if (System.IO.File.Exists(_slocalGeodatabasePath))
{
_localGeodatabase = await Geodatabase.OpenAsync(_slocalGeodatabasePath);
Map geodatabaseMap = new Map();
int beginCount = _localGeodatabase.GeodatabaseFeatureTables.Count - 1;
for (int i = beginCount; i >= 0; i--)
{
GeodatabaseFeatureTable table = _localGeodatabase.GeodatabaseFeatureTables[i];
await table.LoadAsync();
FeatureLayer layer = new FeatureLayer(table);
layer.Name = table.TableName;
geodatabaseMap.OperationalLayers.Add(layer);
_featureLayer = layer;
}
Viewpoint geodatabaseViewPoint = new Viewpoint(_featureLayer.FullExtent);
geodatabaseMap.InitialViewpoint = geodatabaseViewPoint;
MainMapView.Map = geodatabaseMap;
}
}
private void Menu_OpenGeodatabase_Click(object sender, RoutedEventArgs e)
{
OpenGeoDatabase();
}
private void MainMapView_OnViewpointChanged(object? sender, EventArgs e)
{
EagleMapView.GraphicsOverlays.Clear();
Esri.ArcGISRuntime.Geometry.Polygon vExtent = MainMapView.VisibleArea;
Envelope eagleViewEnvelop = vExtent.Extent;
Color lineColor = Color.FromArgb(255, 255, 0, 0);
SimpleLineSymbol lineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol(SimpleLineSymbolStyle.Dash, lineColor,2.0);
Color fillColor = Color.FromArgb(0, 0, 0, 0);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, fillColor, lineSymbol);
var graphicOverlay = new Esri.ArcGISRuntime.UI.GraphicsOverlay();
var envGraphic = new Esri.ArcGISRuntime.UI.Graphic(eagleViewEnvelop, fillSymbol);
graphicOverlay.Graphics.Add(envGraphic);
EagleMapView.GraphicsOverlays.Add(graphicOverlay);
}
private double GetDistance(MapPoint p1, MapPoint p2)
{
double dx = p2.X - p1.X;
double dy = p2.Y - p1.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
private double GetAngle(MapPoint p1, MapPoint p2)
{
double dx = p2.X - p1.X;
double dy = p2.Y - p1.Y;
return Math.Atan2(dy, dx) * 180 / Math.PI;
}
private void Menu_Zoom_in_OnClick(object sender, RoutedEventArgs e)
{
try
{
Viewpoint initViewpoint = MainMapView.GetCurrentViewpoint(ViewpointType.CenterAndScale);
MainMapView.SetViewpointScaleAsync(initViewpoint.TargetScale / 2.0);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
private void Menu_Zoom_out_OnClick(object sender, RoutedEventArgs e)
{
try
{
Viewpoint initViewpoint = MainMapView.GetCurrentViewpoint(ViewpointType.CenterAndScale);
MainMapView.SetViewpointScaleAsync(initViewpoint.TargetScale * 2.0);
Console.WriteLine(initViewpoint.TargetScale);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
private void Menu_OpenOnlineMap_OnClick(object sender, RoutedEventArgs e)
{
OpenOnlineData();
}
private async void OpenOnlineData()
{
Map pMap = new Map();
pMap.Basemap=new Basemap(BasemapStyle.ArcGISTopographicBase);
_featureTable = new ServiceFeatureTable(new Uri(_statesUrl));
_featureLayer = new FeatureLayer(_featureTable);
_featureLayer.MaxScale = 1.0;
await _featureLayer.LoadAsync();
Viewpoint pVP = new Viewpoint(_featureLayer.FullExtent);
pMap.InitialViewpoint = pVP;
SimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.Blue, 5.0);
SimpleLineSymbol pLineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 5.0);
SimpleFillSymbol pFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Pink, pLineSymbol);
SimpleRenderer pRenderer = new SimpleRenderer();
if (_featureLayer.FeatureTable.GeometryType == GeometryType.Polygon)
{
pRenderer.Symbol = pFillSymbol;
}else if (_featureLayer.FeatureTable.GeometryType == GeometryType.Point)
{
pRenderer.Symbol = pMarkerSymbol;
}else if (_featureLayer.FeatureTable.GeometryType == GeometryType.Polyline)
{
pRenderer.Symbol = pLineSymbol;
}
else
{
pRenderer.Symbol = pFillSymbol;
}
_featureLayer.Renderer = pRenderer;
pMap.OperationalLayers.Add(_featureLayer);
MainMapView.Map = pMap;
MainMapView.SelectionProperties.Color = Color.Cyan;
}
private void BasemapChooser_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedBasemapTitle = e.AddedItems[0].ToString();
MainMapView.Map.Basemap = _basemapOptions[selectedBasemapTitle];
}
private void Menu_Full_Extent_OnClick(object sender, RoutedEventArgs e)
{
MainMapView.SetViewpointScaleAsync(192866676.56141916);
}
private void Menu_Wrap_Around_OnClick(object sender, RoutedEventArgs e)
{
if (Menu_Wrap_Around.Header.Equals("开启漫游"))
{
MainMapView.WrapAroundMode = Esri.ArcGISRuntime.UI.WrapAroundMode.EnabledWhenSupported;
Menu_Wrap_Around.Header = "关闭漫游";
}
else
{
MainMapView.WrapAroundMode = Esri.ArcGISRuntime.UI.WrapAroundMode.Disabled;
Menu_Wrap_Around.Header = "开启漫游";
}
}
}
}