Code Monkey home page Code Monkey logo

corespatial's Introduction

CoreSpatial

English

用途

CoreSpatial是基于.NET Standard v2.1实现的用以读写shapefile的类库,支持投影文件.prj的读写。

nuget

类库 nuget
CoreSpatial NuGet Status
CoreSpatial.Converter NuGet Status

项目信息

  • .NET Standard v2.1
  • 支持shapefile的读写
  • 使用ProjNet支持对.prj文件的读写

使用方法

读:

IFeatureSet fs = FeatureSet.Open(shpPath);
...

写:

fs.Save(shpPath);

创建新的shapefile:

void CreatePoint()
{
	IFeatureSet fs = new FeatureSet(FeatureType.Point);
	var point1 = new GeoPoint(133, 30);
	var point2 = new GeoPoint(133, 32);
	var point3 = new GeoPoint(134, 30);

	var feature1 = new Feature(new Geometry(point1));
	var feature2 = new Feature(new Geometry(point2));
	var feature3 = new Feature(new Geometry(point3));

	fs.Features.Add(feature1);
	fs.Features.Add(feature2);
	fs.Features.Add(feature3);
	fs.Crs = Crs.Wgs84Gcs;

	var dataTable = new DataTable();
	dataTable.Columns.Add("名称", typeof(string));
	dataTable.Columns.Add("id", typeof(int));
	var row1 = dataTable.NewRow();
	var row2 = dataTable.NewRow();
	var row3 = dataTable.NewRow();

	row1[0] = "点1";
	row1[1] = 1;

	row2[0] = "点2";
	row2[1] = 2;
	
	row3[0] = "点3";
	row3[1] = 3;

	dataTable.Rows.Add(row1);
	dataTable.Rows.Add(row2);
	dataTable.Rows.Add(row3);

	fs.AttrTable = dataTable;

	fs.Save("../createNew/point.shp");
}

void CreateMultiPoint()
{
	IFeatureSet fs = new FeatureSet(FeatureType.MultiPoint);

	var point1 = new GeoPoint(133, 30);
	var point2 = new GeoPoint(133, 32);
	var point3 = new GeoPoint(134, 30);

	var point4 = new GeoPoint(134, 34);
	var point5 = new GeoPoint(135, 35);

	var multiPoint1 = new MultiPoint(new List<GeoPoint>()
	{
		point1,point2,point3
	});

	var multiPoint2 = new MultiPoint();
	multiPoint2.Points.Add(point4);
	multiPoint2.Points.Add(point5);
	

	var feature1 = new Feature(new Geometry(multiPoint1));
	var feature2 = new Feature(new Geometry(multiPoint2));

	fs.Features.Add(feature1);
	fs.Features.Add(feature2);
	fs.Crs = Crs.Wgs84Gcs;

	var dataTable = new DataTable();
	dataTable.Columns.Add("名称", typeof(string));
	dataTable.Columns.Add("id", typeof(int));
	var row1 = dataTable.NewRow();
	var row2 = dataTable.NewRow();

	row1[0] = "多点1";
	row1[1] = 1;

	row2[0] = "多点2";
	row2[1] = 2;

	dataTable.Rows.Add(row1);
	dataTable.Rows.Add(row2);

	fs.AttrTable = dataTable;

	fs.Save("../createNew/multipoint.shp");
}

void CreatePolyLine()
{
	
	IFeatureSet fs = new FeatureSet(FeatureType.PolyLine);

	var point1 = new GeoPoint(133, 30);
	var point2 = new GeoPoint(133, 32);
	var point3 = new GeoPoint(134, 30);

	var point4 = new GeoPoint(134, 34);
	var point5 = new GeoPoint(135, 35);

	//line ring
	var polyLine1 = new PolyLine(new List<GeoPoint>()
	{
		point1,point2,point3,point1
	});
	var isLineRing = polyLine1.IsLineRing;

	var polyLine2 = new PolyLine(new List<GeoPoint>()
	{
		point4,point5
	});
	
	var feature1 = new Feature(new Geometry(polyLine1));
	var feature2 = new Feature(new Geometry(polyLine2));

	fs.Features.Add(feature1);
	fs.Features.Add(feature2);
	fs.Crs = Crs.Wgs84Gcs;

	var dataTable = new DataTable();
	dataTable.Columns.Add("名称", typeof(string));
	dataTable.Columns.Add("id", typeof(int));
	var row1 = dataTable.NewRow();
	var row2 = dataTable.NewRow();

	row1[0] = "线1";
	row1[1] = 1;

	row2[0] = "线2";
	row2[1] = 2;

	dataTable.Rows.Add(row1);
	dataTable.Rows.Add(row2);

	fs.AttrTable = dataTable;

	fs.Save("../createNew/polyline.shp");
}

void CreateMultiPolyLine()
{
	IFeatureSet fs = new FeatureSet(FeatureType.PolyLine);

	var point1 = new GeoPoint(132, 30);
	var point2 = new GeoPoint(136, 30);
	var point3 = new GeoPoint(134, 28);
	var point4 = new GeoPoint(134, 32);

	var polyLine1 = new PolyLine(new List<GeoPoint>()
	{
		point1,point2
	});

	var polyLine2 = new PolyLine(new List<GeoPoint>()
	{
		point3,point4
	});

	var multiPolyLine1 = new MultiPolyLine(new List<PolyLine>()
	{
		polyLine1,polyLine2
	});
   
	var feature1 = new Feature(new Geometry(multiPolyLine1));

	fs.Features.Add(feature1);
	fs.Crs = Crs.Wgs84Gcs;

	var dataTable = new DataTable();
	dataTable.Columns.Add("名称", typeof(string));
	dataTable.Columns.Add("id", typeof(int));
	var row1 = dataTable.NewRow();

	row1[0] = "十字多线";
	row1[1] = 1;

	dataTable.Rows.Add(row1);

	fs.AttrTable = dataTable;

	fs.Save("../createNew/multiPolyLine.shp");
}

void CreatePolygon()
{
	IFeatureSet fs = new FeatureSet(FeatureType.Polygon);

	var point1 = new GeoPoint(132, 30);
	var point2 = new GeoPoint(136, 30);
	var point3 = new GeoPoint(132, 35);
	
	var edge = new PolyLine(new List<GeoPoint>()
	{
		point1, point2, point3, point1
	});
	
	var polygon = new Polygon(edge);

	//edge,Clockwise
	var point4 = new GeoPoint(140, 30);
	var point5 = new GeoPoint(140, 35);
	var point6 = new GeoPoint(150, 35);
	var point7 = new GeoPoint(150, 30);
	var edge2 = new PolyLine(new List<GeoPoint>()
	{
		point4, point5, point6, point7, point4
	});

	//points sketch 
	//              .(point9)
	//
	//  .(point8)          .(point10)
	//
	var point8 = new GeoPoint(142, 31);
	var point9 = new GeoPoint(145, 33);
	var point10 = new GeoPoint(148, 31);

	//hole, need Counterclockwise,
	//if Counterclockwise, sequence should be:
	//point8, point10, point9
	//It will be handled inside the library.
	//It will be a line ring inside the library too
	var hole = new PolyLine(new List<GeoPoint>()
	{
		point8,point9,point10
	});
	var polygon2 = new Polygon(edge2, hole);


	var feature1 = new Feature(new Geometry(polygon));
	var feature2 = new Feature(new Geometry(polygon2));

	fs.Features.Set(new List<IFeature>()
	{
		feature1,
		feature2
	});
	fs.Crs = Crs.Wgs84Gcs;


	var dataTable = new DataTable();
	dataTable.Columns.Add("名称", typeof(string));
	dataTable.Columns.Add("id", typeof(int));
	var row1 = dataTable.NewRow();
	var row2 = dataTable.NewRow();

	row1[0] = "简单面";
	row1[1] = 1;

	row2[0] = "带洞面";
	row2[1] = 2;

	dataTable.Rows.Add(row1);
	dataTable.Rows.Add(row2);

	fs.AttrTable = dataTable;

	fs.Save("../createNew/polygon.shp");
}

//for using GB2312
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

CreatePoint();
CreateMultiPoint();
CreatePolyLine();
CreateMultiPolyLine();
CreatePolygon();

CoreSpatial参考于DotSpatial,部分用法类似,详见 CoreSpatial.Test。

转为GeoJSON(需要先引用 CoreSpatial.Converter):

fs.ToGeoJSON();

转为KML/KMZ(需要先引用 CoreSpatial.Converter):

fs.ToKML(kmlName);
fs.ToKMZ(kmlName, KmzSavePath);

使用方法见 CoreSpatial.Converter.Test,可在http://geojson.io/校验GeoJSON。

注意事项

  • 暂不能读取M值、Z值shp文件

参考

DotSpatial

NetTopologySuite.IO.ShapeFile

开发者

zxyao145

许可协议

有关许可条款的更多信息,请参阅LICENSE.txt。

corespatial's People

Contributors

vgisnetadmin avatar zxyao145 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

corespatial's Issues

字符编码问题相关

我本地输出的Thread.CurrentThread.CurrentCulture.Name为en-GB

我在CoreSpatial.Test里,添加了代码:

// using gb2312
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

然后得到的DbfEncodingUtil.DefaultEncoding 是null,我不知道为啥,导致我输出中文,一直是乱码。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.