|
[Description("坐标服务接口")]
[ServiceContract]
public interface ICoord2Shapefile
{
//[WebGet(UriTemplate = "{points}", ResponseFormat = WebMessageFormat.Json)]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,UriTemplate="/")]
[Description("将坐标生成shapefile,并压缩打包")]
[OperationContract]
string point2zip(string points);
}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public>
{
public string point2zip(string points)
{
LogHelper.Info(points);
string zip = "";
//List<Location> locs = JsonHelper.DeserializeJsonToList<Location>(points);
try
{
List<Location> locs = new List<Location>();
string[] pps = points.Split(';');
for (int i = 0; i < pps.Length; i++)
{
string[] pp = pps.Split(',');
double x = Convert.ToDouble(pp[0]);
double y = Convert.ToDouble(pp[1]);
Location loc = new Location() { X = x, Y = y, Name = pp[2] };
locs.Add(loc);
}
if (locs != null && locs.Count > 0)
{
LogHelper.Info(string.Format("{0} 个点",locs.Count));
string shpfile = Path.GetTempFileName();
shpfile = shpfile.Substring(0, shpfile.Length - 3) + "shp";
LogHelper.Info(shpfile);
//shpfile = "e:\\aa.shp";
WriteVectorFileShp(shpfile, locs);
if (File.Exists(shpfile))
{
FileInfo fileinfo = new FileInfo(shpfile);
DirectoryInfo dirInfo = fileinfo.Directory;
string shpName = Path.GetFileNameWithoutExtension(shpfile);
List<string> files = new List<string>();
files.Add(shpfile);
files.Add(Path.Combine(dirInfo.FullName, shpName + ".prj"));
files.Add(Path.Combine(dirInfo.FullName, shpName + ".dbf"));
files.Add(Path.Combine(dirInfo.FullName, shpName + ".shx"));
//string zipFile = "d:\\point.zip";
string>
string zipFile = string.Format("d:\\update\\temp\\{0}{1}{2}{3}{4}{5}{6}.zip", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
//string zipFile = string.Format("d:\\{0}{1}{2}{3}{4}{5}{6}.zip", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
//string zipFile = "www.simplemap.com.cn/update/temp";
int ret = ZipHelper.ZipFiles(zipFile, files.ToArray(), "data");
if (ret > 0)
{
foreach (var file in files)
{
try
{
File.Delete(file);
}
catch (Exception)
{
}
}
if (File.Exists(zipFile))
{
FileInfo ff = new FileInfo(zipFile);zip = "http://localhost/temp" + ff.Name;
}
}
}
else
{
LogHelper.Info(string.Format("{0} 不存在!", shpfile));
}
}
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, ex);
}
return zip;
}
static void WriteVectorFileShp(String shapefile_path, List<Location> locs) //创建算法生产的边界矢量图
{
// 为了支持中文路径,请添加下面这句代码
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
// 注册所有的驱动
Ogr.RegisterAll();
LogHelper.Info("注册GDAL成功");
//创建数据,创建ESRI的shp文件
string strDriverName = "ESRI Shapefile";
Driver oDriver = Ogr.GetDriverByName(strDriverName);
if (oDriver == null)
{
Debug.WriteLine("%s 驱动不可用!\n", shapefile_path);
return;
}
// 步骤1、创建数据源
DataSource oDS = oDriver.CreateDataSource(shapefile_path, null);
if (oDS == null)
{
Debug.WriteLine("创建矢量文件【%s】失败!", shapefile_path);
return;
}
LogHelper.Info("创建矢量文件成功");
//步骤2、创建空间坐标系
OSGeo.OSR.SpatialReference oSRS = new OSGeo.OSR.SpatialReference("");
oSRS.SetWellKnownGeogCS("WGS84");
//步骤3、创建图层,并添加坐标系,创建一个多边形图层(wkbGeometryType.wkbUnknown,存放任意几何特征)
Layer oLayer = oDS.CreateLayer("地名", oSRS, wkbGeometryType.wkbPoint, null);
if (oLayer == null)
{
Debug.WriteLine("图层创建失败!");
return;
}
// 步骤4、下面创建属性表
FieldDefn oFieldPlotArea = new FieldDefn("Name", FieldType.OFTString); // 先创建一个叫PlotArea的属性
oFieldPlotArea.SetWidth(100);
// 步骤5、将创建的属性表添加到图层中
oLayer.CreateField(oFieldPlotArea, 1);
//步骤6、定义一个特征要素oFeature(特征要素包含两个方面1.属性特征2.几何特征)
foreach (var loc in locs)
{
FeatureDefn oDefn = oLayer.GetLayerDefn();
Feature oFeature = new Feature(oDefn); //建立了一个特征要素并将指向图层oLayer的属性表
//步骤7、设置属性特征的值
oFeature.SetField(0, loc.Name);
OSGeo.OGR.Geometry pt = new Geometry(wkbGeometryType.wkbPoint);
pt.AddPoint(loc.X, loc.Y, 0);
oFeature.SetGeometry(pt);
//OSGeo.OGR.Geometry geomTriangle = OSGeo.OGR.Geometry.CreateFromWkt(wkt);//创建一个几何特征
//步骤8、设置几何特征
//oFeature.SetGeometry(geomTriangle);
//步骤9、将特征要素添加到图层中
oLayer.CreateFeature(oFeature);
}
oDS.Dispose();
LogHelper.Info("数据集创建完成!");
//Debug.WriteLine("数据集创建完成!");
}
}
public>
{
public double X { get; set; }
public double Y { get; set; }
public string Name { get; set; }
} |
|