|
|
using EmbedIO;
|
|
|
using EmbedIO.Routing;
|
|
|
using EmbedIO.WebApi;
|
|
|
using GeoSigma.SigmaDrawerUtil;
|
|
|
using Swan.Formatters;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Collections.Specialized;
|
|
|
using System.Drawing;
|
|
|
using System.Drawing.Imaging;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace KevServer
|
|
|
{
|
|
|
public class TestJson
|
|
|
{
|
|
|
public int Name { get; set; }
|
|
|
public string Age { get; set; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 画布事件处理 Controller
|
|
|
/// </summary>
|
|
|
internal class DrawController : WebApiController
|
|
|
{
|
|
|
private const string ContentTextType = "text/plain";
|
|
|
private static readonly Object locker = new Object();
|
|
|
|
|
|
/// <summary>
|
|
|
/// 用于测试的接口
|
|
|
/// </summary>
|
|
|
/// <returns>pong</returns>
|
|
|
[Route(HttpVerbs.Get, "/ping")]
|
|
|
public bool TableTennis()
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/status")]
|
|
|
public bool Status()
|
|
|
{
|
|
|
return true;
|
|
|
//return drawer?.Geo != null;
|
|
|
}
|
|
|
|
|
|
public string MapFile { get; set; } = string.Empty;
|
|
|
public string ImageFile { get; set; } = string.Empty;
|
|
|
public string BaseMapFile { get; set; } = string.Empty;
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/Open")]
|
|
|
public async Task<string> OpenAsync([QueryField] string drawId, [QueryField] string file, [QueryField] int w = 0, [QueryField] int h = 0)
|
|
|
{
|
|
|
if (GetCurrentDrawer(drawId) != null)
|
|
|
{
|
|
|
return drawId;
|
|
|
}
|
|
|
|
|
|
return await Task.Run(() =>
|
|
|
{
|
|
|
string strServerPath = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
DrawerConfig.StartupPath = strServerPath;
|
|
|
WebConfig.StartupPath = strServerPath;
|
|
|
strServerPath = WebConfig.Instance.MapDirectory;
|
|
|
MapFile = Path.Combine(strServerPath, file);
|
|
|
ImageFile = Path.Combine(strServerPath, "K1.png");
|
|
|
|
|
|
WebDrawer drawer = null;
|
|
|
|
|
|
lock (locker)
|
|
|
{
|
|
|
drawer = WebDrawer.OpenFile(MapFile);
|
|
|
}
|
|
|
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
var result = new { errorMessage = $"Open ${file} failed" };
|
|
|
HttpContext.SendStringAsync(Json.Serialize(result), ContentTextType, System.Text.Encoding.UTF8);
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
drawer.DrawerSize = new Size(w, h);
|
|
|
|
|
|
Guid guid = Guid.NewGuid();
|
|
|
Session[$"KevDrawer.id.{guid}"] = drawer;
|
|
|
|
|
|
return guid.ToString();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/Redraw")]
|
|
|
public async Task Redraw([QueryField] string drawId, [QueryField] int w = 0, [QueryField] int h = 0)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (w > 0)
|
|
|
{
|
|
|
drawer.DrawerSize = new Size(w, drawer.DrawerSize.Height);
|
|
|
}
|
|
|
|
|
|
await ShowImgAsync(drawer);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获得当前Session中的Drawer.
|
|
|
/// </summary>
|
|
|
/// <returns>A WebDrawer.</returns>
|
|
|
private WebDrawer GetCurrentDrawer(string drawId)
|
|
|
{
|
|
|
if (Session.TryGetValue($"KevDrawer.id.{drawId}", out object oDrawer))
|
|
|
{
|
|
|
return oDrawer as WebDrawer;
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private async Task ShowImgAsync(WebDrawer drawer)
|
|
|
{
|
|
|
using (var bmp = new Bitmap(drawer.DrawerSize.Width, drawer.DrawerSize.Height))
|
|
|
{
|
|
|
lock (locker)
|
|
|
{
|
|
|
Graphics g = Graphics.FromImage(bmp);
|
|
|
drawer.OnPaint(g);
|
|
|
}
|
|
|
//bmp.MakeTransparent(Color.White);
|
|
|
byte[] image = ConvertBitmapToByteArray(bmp);
|
|
|
//byte[] image = GetBytesFromImage(ImageFile);
|
|
|
await HttpContext.SendStringAsync(Convert.ToBase64String(image), ContentTextType, System.Text.Encoding.UTF8);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/SaveFile")]
|
|
|
public async Task<bool> SaveFileAsync([QueryField] string drawId)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return await Task.Run(() =>
|
|
|
{
|
|
|
var savePath = Path.Combine(Environment.CurrentDirectory, drawer.DrawFileName);
|
|
|
return drawer.Geo.SaveDocument(savePath);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Delete, "/DeleteFile")]
|
|
|
public async Task<bool> DeleteFileAsync([QueryField] string drawId)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return await Task.Run(() =>
|
|
|
{
|
|
|
var filePath = Path.Combine(Environment.CurrentDirectory, drawer.DrawFileName);
|
|
|
drawer.Dispose();
|
|
|
File.Delete(filePath);
|
|
|
return true;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/ZoomIn")]
|
|
|
public async Task ZoomIn([QueryField] string drawId)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
drawer.ZoomIn();
|
|
|
await ShowImgAsync(drawer);
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/ZoomOut")]
|
|
|
public async Task ZoomOut([QueryField] string drawId)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
drawer.ZoomOut();
|
|
|
await ShowImgAsync(drawer);
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/ViewAll")]
|
|
|
public async Task ViewAll([QueryField] string drawId)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
drawer.ViewAll();
|
|
|
await ShowImgAsync(drawer);
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/SelectAll")]
|
|
|
public async System.Threading.Tasks.Task SelectAll([QueryField] string drawId, [QueryField] int arg)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
WebDrawToolSelect drawToolSelect = new WebDrawToolSelect(drawer.Geo);
|
|
|
drawToolSelect.Start();
|
|
|
drawToolSelect.SelectAll();
|
|
|
await ShowImgAsync(drawer);
|
|
|
}
|
|
|
|
|
|
public static byte[] ConvertBitmapToByteArray(Bitmap bitmap)
|
|
|
{
|
|
|
using (MemoryStream memoryStream = new MemoryStream(bitmap.Width * bitmap.Height * 4))
|
|
|
{
|
|
|
// 选择图像格式,可以是Png、Jpeg等
|
|
|
bitmap.Save(memoryStream, ImageFormat.Png);
|
|
|
return memoryStream.ToArray();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 选中对象
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Route(HttpVerbs.Get, "/ClickImage")]
|
|
|
public async Task<ActionResult> ClickImageAsync([QueryField] string drawId, [QueryField] double x, [QueryField] double y)
|
|
|
{
|
|
|
Console.WriteLine("LbuttonDown");
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return new ActionResult(0, null, 0, 0, 0, 0);
|
|
|
}
|
|
|
Bitmap bmp = drawer.OnLButtonDown(x, y);
|
|
|
if (bmp == null)
|
|
|
{
|
|
|
return new ActionResult(0, null, 0, 0, 0, 0);
|
|
|
}
|
|
|
|
|
|
return await Task.Run(() =>
|
|
|
{
|
|
|
bmp.MakeTransparent(Color.White);
|
|
|
Point pt = drawer.CacheImgLocation;
|
|
|
byte[] image = ConvertBitmapToByteArray(bmp);
|
|
|
ActionResult result = new ActionResult(1, Convert.ToBase64String(image), pt.X, pt.Y, bmp.Width, bmp.Height);
|
|
|
return result;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/LButtonUp")]
|
|
|
public async Task LButtonUp([QueryField] string drawId, [QueryField] double x, [QueryField] double y)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
using (var bmp = drawer.LButtonUp(x, y))
|
|
|
{
|
|
|
if (bmp != null)
|
|
|
{
|
|
|
byte[] image = ConvertBitmapToByteArray(bmp);
|
|
|
await HttpContext.SendStringAsync(Convert.ToBase64String(image), ContentTextType, System.Text.Encoding.UTF8);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/TranslateElements")]
|
|
|
public async Task TranslateElements([QueryField] string drawId, [QueryField] double x, [QueryField] double y)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
Bitmap bmp = drawer.TranslateElements(x, y);
|
|
|
if (bmp != null)
|
|
|
{
|
|
|
byte[] image = ConvertBitmapToByteArray(bmp);
|
|
|
await HttpContext.SendStringAsync(Convert.ToBase64String(image), ContentTextType, System.Text.Encoding.UTF8);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/MouseMove")]
|
|
|
public async Task<int> MouseMove([QueryField] string drawId, [QueryField] double x, [QueryField] double y)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
|
return await Task.Run(() =>
|
|
|
{
|
|
|
lock (locker)
|
|
|
{
|
|
|
return drawer.MouseMove(x, y);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/ViewOperator")]
|
|
|
public async Task ViewOperator([QueryField] string drawId, [QueryField] double startX, [QueryField] double startY, [QueryField] double endX, [QueryField] double endY)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
using (var bmp = drawer.ViewOperator(startX, startY, endX, endY))
|
|
|
{
|
|
|
if (bmp != null)
|
|
|
{
|
|
|
byte[] image = ConvertBitmapToByteArray(bmp);
|
|
|
await HttpContext.SendStringAsync(Convert.ToBase64String(image), ContentTextType, System.Text.Encoding.UTF8);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public byte[] GetBytesFromImage(string filename)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
|
|
|
int length = (int)fs.Length;
|
|
|
byte[] image = new byte[length];
|
|
|
fs.Read(image, 0, length);
|
|
|
fs.Close();
|
|
|
return image;
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#region 图层管理
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获得图层数据.
|
|
|
/// </summary>
|
|
|
/// <returns>A Task.</returns>
|
|
|
[Route(HttpVerbs.Get, "/GetLayers")]
|
|
|
public async Task GetLayers([QueryField] string drawId)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null || drawer.LayerTree == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var layersTemp1 = drawer.LayerTree.Layers.Split(new string[] { Environment.NewLine },
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
var layerList = new List<string>();
|
|
|
foreach (var layer in layersTemp1)
|
|
|
{
|
|
|
if (layer.Split('|').Length > 0)
|
|
|
{
|
|
|
layerList.Add(layer.Split('|')[1]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var layerStr = string.Join(Environment.NewLine, layerList);
|
|
|
await HttpContext.SendStringAsync(layerStr, ContentTextType, System.Text.Encoding.UTF8);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Sets the layers status.
|
|
|
/// </summary>
|
|
|
/// <param name="layersStatus">The layers status.
|
|
|
/// VIEW_EDIT = 10,
|
|
|
/// VIEW_NOT_EDIT = 11,
|
|
|
/// NOT_VIEW_NOT_EDIT = 12</param>
|
|
|
/// <returns>A Task.</returns>
|
|
|
[Route(HttpVerbs.Get, "/SetLayersStatus")]
|
|
|
public async Task SetLayersStatus([QueryField] string drawId, [QueryField] string layersStatus)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
GeoLayerTree layerTree = drawer.LayerTree;
|
|
|
if (layerTree.SetLayersStatus(layersStatus))
|
|
|
{
|
|
|
await ShowImgAsync(drawer);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除图层
|
|
|
/// </summary>
|
|
|
/// <param name="layers"></param>
|
|
|
/// <param name="withSubLayer"></param>
|
|
|
/// <returns></returns>
|
|
|
[Route(HttpVerbs.Delete, "/DeleteLayers")]
|
|
|
public async Task DeleteLayers([QueryField] string drawId, [QueryField] string layers, [QueryField] bool withSubLayer)
|
|
|
{
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var layerArray = layers.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
await Task.Run(() =>
|
|
|
{
|
|
|
lock (locker)
|
|
|
{
|
|
|
drawer.DeleteLayers(layerArray, withSubLayer);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
#endregion 图层管理
|
|
|
|
|
|
#region 底图管理
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加底图
|
|
|
/// </summary>
|
|
|
/// <param name="fileUrl">文件路径</param>
|
|
|
/// <param name="fileName">文件路径</param>
|
|
|
/// <returns></returns>
|
|
|
[Route(HttpVerbs.Post, "/AddBasicDraw")]
|
|
|
public async Task<string> AddBasicDraw([QueryField] string fileUrl, [QueryField] string fileName)
|
|
|
{
|
|
|
var strServerPath = WebConfig.Instance.BaseMapDirectory;
|
|
|
if (!Directory.Exists(strServerPath))
|
|
|
{
|
|
|
Directory.CreateDirectory(strServerPath);
|
|
|
}
|
|
|
|
|
|
try
|
|
|
{
|
|
|
fileName = fileName + "" + Path.GetExtension(fileUrl);
|
|
|
BaseMapFile = Path.Combine(strServerPath, fileName);
|
|
|
|
|
|
if (fileUrl.StartsWith("http://") || fileUrl.StartsWith("https://"))
|
|
|
{
|
|
|
using (var client = new WebClient())
|
|
|
{
|
|
|
await client.DownloadFileTaskAsync(fileUrl, BaseMapFile);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
File.Copy(fileUrl, BaseMapFile, true);
|
|
|
}
|
|
|
return File.Exists(BaseMapFile) ? fileName : string.Empty;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine(e);
|
|
|
}
|
|
|
|
|
|
return string.Empty;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 底图列表
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Route(HttpVerbs.Get, "/ListBasicDraw")]
|
|
|
public List<string> GetBasicDrawList()
|
|
|
{
|
|
|
var strServerPath = WebConfig.Instance.BaseMapDirectory;
|
|
|
return Directory.EnumerateFiles(strServerPath).ToList();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除底图
|
|
|
/// </summary>
|
|
|
/// <param name="filePaths"></param>
|
|
|
/// <returns></returns>
|
|
|
[Route(HttpVerbs.Delete, "/DeleteBasicDraw")]
|
|
|
public async Task<bool> DeleteBasicDraw([FormData] NameValueCollection data)
|
|
|
{
|
|
|
var baseDrawFiles = data.Get("baseDrawFiles");
|
|
|
if (baseDrawFiles == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
var strServerPath = WebConfig.Instance.BaseMapDirectory;
|
|
|
foreach (var s in Directory.GetFiles(strServerPath))
|
|
|
{
|
|
|
if (baseDrawFiles.Contains(Path.GetFileName(s)))
|
|
|
{
|
|
|
File.Delete(s);
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/DecodeOverrideFile")]
|
|
|
public bool DecodeOverrideFile([QueryField] string fileName)
|
|
|
{
|
|
|
//获取原路径目录
|
|
|
if (GeoSigmaDrawLib.FileUtility.IsTextFile(fileName))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
if (GeoSigmaDrawLib.FileUtility.DecodeFile(fileName, out string tempFileName))
|
|
|
{
|
|
|
File.Copy(tempFileName, fileName, true);
|
|
|
File.Delete(tempFileName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
[Route(HttpVerbs.Get, "/EncodeFile")]
|
|
|
public bool EncodeFile([QueryField] string fileName)
|
|
|
{
|
|
|
if (!GeoSigmaDrawLib.FileUtility.IsTextFile(fileName))
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
if (!fileName.Contains(".kev") && !Path.GetExtension(fileName).Equals(".kev"))
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
string tempFileName = Path.Combine(Path.GetDirectoryName(fileName), Guid.NewGuid().ToString());
|
|
|
//获取原路径目录
|
|
|
if (GeoSigmaDrawLib.FileUtility.EncodeFile(fileName, tempFileName))
|
|
|
{
|
|
|
File.Copy(tempFileName, fileName, true);
|
|
|
File.Delete(tempFileName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 数据操作
|
|
|
|
|
|
[Route(HttpVerbs.Post, "/AddBufferData")]
|
|
|
public async Task<int> AddBufferData([FormData] NameValueCollection data)
|
|
|
{
|
|
|
string drawId = data.Get("drawId");
|
|
|
if (drawId == null)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
var drawer = GetCurrentDrawer(drawId);
|
|
|
if (drawer == null)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
return await Task.Run(() =>
|
|
|
{
|
|
|
lock (locker)
|
|
|
{
|
|
|
return drawer.AddBufferData(data.Get("data"));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
#endregion 数据操作
|
|
|
}
|
|
|
|
|
|
public class ActionResult
|
|
|
{
|
|
|
public ActionResult(int resultType, string data, int imgLeft, int imgTop, int imgWidth, int imgHeight)
|
|
|
{
|
|
|
ResultType = resultType;
|
|
|
Data = data;
|
|
|
ImgLeft = imgLeft;
|
|
|
ImgTop = imgTop;
|
|
|
ImgWidth = imgWidth;
|
|
|
ImgHeight = imgHeight;
|
|
|
}
|
|
|
|
|
|
public int ResultType { get; set; } = 0;
|
|
|
public string Data { get; set; } = string.Empty;
|
|
|
public int ImgLeft { get; set; }
|
|
|
public int ImgTop { get; set; }
|
|
|
public int ImgWidth { get; set; }
|
|
|
public int ImgHeight { get; set; }
|
|
|
}
|
|
|
} |