using Microsoft.ClearScript;
|
using Microsoft.ClearScript.V8;
|
using Npgsql;
|
using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Data.Common;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Web;
|
using Turf.Models;
|
|
namespace Turf.cs
|
{
|
public class Tools
|
{
|
public Tools() { }
|
|
private V8Script v8Script;
|
|
public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
private static PostgreHelper _dbHelper;
|
|
public static PostgreHelper DBHelper
|
{
|
get
|
{
|
if (null == _dbHelper)
|
{
|
_dbHelper = new PostgreHelper();
|
}
|
|
return _dbHelper;
|
}
|
}
|
|
public static string selectCoordinatesByCodeSql = "select ST_X(geom) x, ST_Y(geom) y from bs.s_explorationpoint b where dirid like @code and geom is not null";
|
|
public static string selectCoordinates(string code)
|
{
|
DbParameter dp = new NpgsqlParameter("@code", code + "%");
|
|
DataTable dt = DBHelper.GetDataTable(selectCoordinatesByCodeSql, dp);
|
if (null == dt || 0 == dt.Rows.Count) return null;
|
|
List<Coordinate> list = ModelHandler.FillModel<Coordinate>(dt);
|
if (null == list || 0 == list.Count) return null;
|
|
StringBuilder sb = new StringBuilder();
|
sb.Append("[");
|
foreach (Coordinate cs in list)
|
{
|
sb.Append(cs.ToString() + ",");
|
}
|
sb.Remove(sb.Length - 1, 1);
|
sb.Append(']');
|
|
return sb.ToString();
|
}
|
|
public static double CalcArea(string code)
|
{
|
using (V8ScriptEngine engine = new V8ScriptEngine())
|
{
|
engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading;
|
engine.DefaultAccess = ScriptAccess.Full;
|
|
string jsFile = Path.Combine(BaseDir, "js\\turf.min.6.5.js");
|
V8Script script = engine.CompileDocument(jsFile);
|
engine.Execute(script);
|
|
string cs = selectCoordinates(code);
|
if (string.IsNullOrEmpty(cs)) return 0;
|
|
object obj = engine.Invoke("pointsToPolygon", cs);
|
|
return null == obj ? 0 : Convert.ToDouble(obj);
|
}
|
}
|
}
|
}
|