1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/******************************************************************************
* $Id: OGRLayerAlg.cs 27014 2014-03-06 20:06:32Z tamas $
*
* Name: OGRLayerAlg.cs
* Project: GDAL CSharp Interface
* Purpose: A sample app to demonstrate the usage of the RFC-39 functions.
* Author: Tamas Szekeres, szekerest@gmail.com
*
******************************************************************************
* Copyright (c) 2013, Tamas Szekeres
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
using System;
using OSGeo.OGR;
/**
* <p>Title: OGR C# OGRLayerAlg example.</p>
* <p>Description: A sample app to demonstrate the usage of the RFC-39 functions.</p>
* @author Tamas Szekeres (szekerest@gmail.com)
* @version 1.0
*/
/// <summary>
/// A sample app to demonstrate the usage of the RFC-39 functions.
/// </summary>
class OGRLayerAlg {
public static void usage()
{
Console.WriteLine("usage: ogrlayeralg {function} {Data Source 1} {layer1} {Data Source 2} {layer2} {Result Data Source Name} {Result Layer Name} [{Options}]");
Console.WriteLine("example: ogrlayeralg Union data1.shp layer1 data.shp layer2 result.shp resultlayer SKIP_FAILURES=YES");
System.Environment.Exit(-1);
}
public static void Main(string[] args)
{
if (args.Length <= 6) usage();
Console.WriteLine("");
try
{
/* -------------------------------------------------------------------- */
/* Register driver(s). */
/* -------------------------------------------------------------------- */
Ogr.RegisterAll();
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
DataSource ds1 = Ogr.Open(args[1], 0);
if (ds1 == null)
{
Console.WriteLine("Can't open " + args[1]);
System.Environment.Exit(-1);
}
Layer layer1 = ds1.GetLayerByName(args[2]);
if (layer1 == null)
{
Console.WriteLine("FAILURE: Couldn't fetch layer " + args[2]);
System.Environment.Exit(-1);
}
DataSource ds2 = Ogr.Open(args[3], 0);
if (ds2 == null)
{
Console.WriteLine("Can't open " + args[3]);
System.Environment.Exit(-1);
}
Layer layer2 = ds2.GetLayerByName(args[4]);
if (layer2 == null)
{
Console.WriteLine("FAILURE: Couldn't fetch layer " + args[4]);
System.Environment.Exit(-1);
}
/* -------------------------------------------------------------------- */
/* Get driver for creating the result ds */
/* -------------------------------------------------------------------- */
Driver drv = Ogr.GetDriverByName("ESRI Shapefile");
if (drv == null)
{
Console.WriteLine("Can't get driver.");
System.Environment.Exit(-1);
}
/* -------------------------------------------------------------------- */
/* Creating the datasource */
/* -------------------------------------------------------------------- */
DataSource ds = drv.CreateDataSource( args[5], new string[] {} );
if (drv == null)
{
Console.WriteLine("Can't create the datasource.");
System.Environment.Exit(-1);
}
/* -------------------------------------------------------------------- */
/* Process options */
/* -------------------------------------------------------------------- */
string[] options = null;
if (args.Length > 7)
{
options = new string[args.Length - 7];
Array.Copy(args, 7, options, 0, args.Length - 7);
}
/* -------------------------------------------------------------------- */
/* Creating the layer */
/* -------------------------------------------------------------------- */
Layer layer;
int i;
for(i=0;i<ds.GetLayerCount();i++)
{
layer = ds.GetLayerByIndex( i );
if( layer != null && layer.GetLayerDefn().GetName() == args[6])
{
Console.WriteLine("Layer already existed. Recreating it.\n");
ds.DeleteLayer(i);
break;
}
}
layer = ds.CreateLayer(args[6], null, layer1.GetLayerDefn().GetGeomType(), new string[] { });
if( layer == null )
{
Console.WriteLine("Layer creation failed.");
System.Environment.Exit(-1);
}
switch(args[0])
{
case "Intersection":
layer1.Intersection(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "Intersection");
break;
case "Union":
layer1.Union(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "Union");
break;
case "SymDifference":
layer1.SymDifference(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "SymDifference");
break;
case "Identity":
layer1.Identity(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "Identity");
break;
case "Update":
layer1.Update(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "Update");
break;
case "Clip":
layer1.Clip(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "Clip");
break;
case "Erase":
layer1.Erase(layer2, layer, options, new Ogr.GDALProgressFuncDelegate(ProgressFunc), "Erase");
break;
}
}
catch (Exception e)
{
Console.WriteLine("Application error: " + e.Message);
}
}
public static int ProgressFunc(double Complete, IntPtr Message, IntPtr Data)
{
Console.Write("Processing ... " + Complete * 100 + "% Completed.");
if (Message != IntPtr.Zero)
Console.Write(" Message:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Message));
if (Data != IntPtr.Zero)
Console.Write(" Data:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Data));
Console.WriteLine("");
return 1;
}
}
|