Geometry对象操作 - 创建对象

本文是Python GDAL/OGR Cookbook 1.0的C#版代码,主要关注和几何对象有关的操作

说明

  • 在运行下列代码前,需要加入GDAL/OGR的引用,并导入命名空间using OSGeo.OGR;并注册驱动器
  • 定义了PrintGeometry函数,用于打印输出几何对象,在代码中不再一一调用

private static void PrintGeometry(Geometry geo)
{
Console.WriteLine(geo.ExportToJson(null));
string strWkt;
geo.ExportToWkt(out strWkt);
Console.WriteLine(strWkt);
}

  • Create a Point
Geometry geo = new Geometry(wkbGeometryType.wkbPoint);
geo.AddPoint_2D(1198054.34, 648493.09);
  • Create a LineString
Geometry geo = new Geometry(wkbGeometryType.wkbLineString);
geo.AddPoint_2D(1116651.439379124, 637392.6969887456);
geo.AddPoint_2D(1188804.0108498496, 652655.7409537067);
geo.AddPoint_2D(1226730.3625203592, 634155.0816022386);
geo.AddPoint_2D(1281307.30760719, 636467.6640211721); 
  • Create a Polygon
Geometry ring = new Geometry(wkbGeometryType.wkbLinearRing);
ring.AddPoint_2D(1179091.1646903288, 712782.8838459781);
ring.AddPoint_2D(1161053.0218226474, 667456.2684348812);
ring.AddPoint_2D(1214704.933941905, 641092.8288590391);
ring.AddPoint_2D(1228580.428455506, 682719.3123998424);
ring.AddPoint_2D(1218405.0658121984, 721108.1805541387);
ring.AddPoint_2D(1179091.1646903288, 712782.8838459781);
Geometry geo = new Geometry(wkbGeometryType.wkbPolygon);
geo.AddGeometry(ring);
  • Create a Polygon with holes
Geometry outRing = new Geometry(wkbGeometryType.wkbLinearRing);
outRing.AddPoint_2D(1154115.274565847, 686419.4442701361);
outRing.AddPoint_2D(1154115.274565847, 653118.2574374934);
outRing.AddPoint_2D(1165678.1866605144, 653118.2574374934);
outRing.AddPoint_2D(1165678.1866605144, 686419.4442701361);
outRing.AddPoint_2D(1154115.274565847, 686419.4442701361);
Geometry innerRing = new Geometry(wkbGeometryType.wkbLinearRing);
innerRing.AddPoint_2D(1149490.1097279799, 691044.6091080031);
innerRing.AddPoint_2D(1149490.1097279799, 648030.5761158396);
innerRing.AddPoint_2D(1191579.1097525698, 648030.5761158396);
innerRing.AddPoint_2D(1191579.1097525698, 691044.6091080031);
innerRing.AddPoint_2D(1149490.1097279799, 691044.6091080031);
Geometry geo = new Geometry(wkbGeometryType.wkbPolygon);
geo.AddGeometry(outRing);
geo.AddGeometry(innerRing);
  • Create a MultiPoint
Geometry point1 = new Geometry(wkbGeometryType.wkbPoint);
point1.AddPoint_2D(1251243.7361610543, 598078.7958668759);
Geometry point2 = new Geometry(wkbGeometryType.wkbPoint);
point2.AddPoint_2D(1240605.8570339603, 601778.9277371694);
Geometry point3 = new Geometry(wkbGeometryType.wkbPoint);
point3.AddPoint_2D(1250318.7031934808, 606404.0925750365);
Geometry geo = new Geometry(wkbGeometryType.wkbMultiPoint);
geo.AddGeometry(point1);
geo.AddGeometry(point2);
geo.AddGeometry(point3);
  • Create a MultiLineString
Geometry line1 = new Geometry(wkbGeometryType.wkbLineString);
line1.AddPoint_2D(1214242.4174581182, 617041.9717021306);
line1.AddPoint_2D(1234593.142744733, 629529.9167643716);
Geometry line2 = new Geometry(wkbGeometryType.wkbLineString);
line1.AddPoint_2D(1184641.3624957693, 626754.8178616514);
line1.AddPoint_2D(1219792.6152635587, 606866.6090588232);
Geometry geo = new Geometry(wkbGeometryType.wkbMultiLineString);
geo.AddGeometry(line1);
geo.AddGeometry(line2);
  • Create a MultiPolygon
Geometry ring1 = new Geometry(wkbGeometryType.wkbLinearRing);
ring1.AddPoint_2D(1204067.0548148106, 634617.5980860253);
ring1.AddPoint_2D(1204067.0548148106, 620742.1035724243);
ring1.AddPoint_2D(1215167.4504256917, 620742.1035724243);
ring1.AddPoint_2D(1215167.4504256917, 634617.5980860253);
ring1.AddPoint_2D(1204067.0548148106, 634617.5980860253);
Geometry poly1 = new Geometry(wkbGeometryType.wkbPolygon);
poly1.AddGeometry(ring1);
Geometry ring2 = new Geometry(wkbGeometryType.wkbLinearRing);
ring2.AddPoint_2D(1179553.6811741155, 647105.5431482664);
ring2.AddPoint_2D(1179553.6811741155, 626292.3013778647);
ring2.AddPoint_2D(1194354.20865529, 626292.3013778647);
ring2.AddPoint_2D(1194354.20865529, 647105.5431482664);
ring2.AddPoint_2D(1179553.6811741155, 647105.5431482664);
Geometry poly2 = new Geometry(wkbGeometryType.wkbPolygon);
poly2.AddGeometry(ring2);
Geometry geo = new Geometry(wkbGeometryType.wkbMultiPolygon);
geo.AddGeometry(poly1);
geo.AddGeometry(poly2);
  • Create a GeometryCollection
Geometry point = new Geometry(wkbGeometryType.wkbPoint);
point.AddPoint_2D(-122.23, 47.09);
Geometry line = new Geometry(wkbGeometryType.wkbLineString);
line.AddPoint_2D(-122.60, 47.14);
line.AddPoint_2D(-122.48, 47.23);
Geometry geo = new Geometry(wkbGeometryType.wkbGeometryCollection);
geo.AddGeometry(point);
geo.AddGeometry(line);
  • Create Geometry from WKT
string wkt = "POINT (1120351.5712494177 741921.4223245403)";
Geometry geo0 = Ogr.CreateGeometryFromWkt(ref wkt, null);
Console.WriteLine("{0}, {1}", geo0.GetX(0), geo0.GetY(0));
  • Create Geometry from GeoJSON
string geojson = "{\\"type\\":\\"Point\\",\\"coordinates\\":[108420.33,753808.59]}";
Geometry geo1 = Ogr.CreateGeometryFromJson(geojson);
Console.WriteLine("{0}, {1}", geo1.GetX(0), geo1.GetY(0));
  • Create Geometry from GML
string gml = "108420.33,753808.59";
Geometry geo2 = Ogr.CreateGeometryFromGML(gml);
Console.WriteLine("{0}, {1}", geo2.GetX(0), geo2.GetY(0));
  • Create Geometry from WKB
byte[] wkb = new byte[] { 
    0,0,0,0,3,0,0,0,1,0,0,0,6,65,49,253,211,42,41,37,56,65,37,192,157,196,135,117,19,
    7,65,49,183,93,5,150,43,69,65,36,94,128,137,112,75,248,65,50,136,240,239,22,209,
    18,65,35,144,137,168,96,54,68,65,50,191,36,109,175,66,146,65,36,213,190,159,242,
    223,69,65,50,151,101,16,217,17,120,65,38,1,168,92,113,151,146,65,49,253,211,42,4,
    1,37,56,65,37,192,157,196,135,117,197
};
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(wkb, 0);
Geometry geo = Ogr.CreateGeometryFromWkb(wkb.Length, ptr, null);
PrintGeometry(geo);
  • Write Geometry to GeoJSON
...
Console.WriteLine(poly.ExportToJson(null));
  • Write Geometry to WKT
...
string outWkt;
poly.ExportToWkt(out outWkt);
Console.WriteLine(outWkt);
  • Write Geometry to KML
...
Console.WriteLine(poly.ExportToKML(null));
  • Write Geometry to WKB
...
byte[] outWkb = new byte[1000];
poly.ExportToWkb(outWkb);

你可能感兴趣的:(Geometry对象操作 - 创建对象)