C# GDAL基础使用之矢量处理3 要素编辑(OGRFeatureEdit)

31 篇文章 8 订阅
这个C#代码示例展示了如何利用GDAL库来更新和删除OGR数据源中的特征。程序首先注册所有GDAL格式,然后打开指定的数据源和图层,接着根据命令行参数执行更新、复制或删除操作。如果特征存在,它会进行相应的操作,并给出操作结果反馈。
摘要由CSDN通过智能技术生成
/******************************************************************************
 * $Id$
 *
 * Name:     OGRFeatureEdit.cs
 * Project:  GDAL CSharp Interface
 * Purpose:  Sample application to update/delete feature.
 * Author:   Tamas Szekeres, szekerest@gmail.com
 *
 ******************************************************************************
 * Copyright (c) 2015, 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;
using OSGeo.OSR;


/// <summary>
/// Sample application to update/delete feature.
/// </summary> 

class OGRFeatureEdit
{
	public static void usage() 
	{
        Console.WriteLine("usage:");
        Console.WriteLine("OGRFeatureEdit {delete} {data source name} {layer name} {fid}");
        Console.WriteLine("OGRFeatureEdit {update} {data source name} {layer name} {fid} {fieldname} {new value}");
        Console.WriteLine("OGRFeatureEdit {copy} {data source name} {layer name} {src_fid} {dst_fid}");
		System.Environment.Exit(-1);
	}
 
	public static void Main(string[] args) {

		if (args.Length < 4) usage();

        // Using early initialization of System.Console
        Console.WriteLine("");

		/* -------------------------------------------------------------------- */
		/*      Register format(s).                                             */
		/* -------------------------------------------------------------------- */
		Ogr.RegisterAll();

		/* -------------------------------------------------------------------- */
		/*      Open data source.                                               */
		/* -------------------------------------------------------------------- */
		DataSource ds = Ogr.Open( args[1], 1 );
		
		if (ds == null) {
			Console.WriteLine("Can't open " + args[0]);
			System.Environment.Exit(-1);
		}

		/* -------------------------------------------------------------------- */
		/*      Get driver                                                      */
		/* -------------------------------------------------------------------- */	
		Driver drv = ds.GetDriver();

		if (drv == null) 
		{
			Console.WriteLine("Can't get driver.");
			System.Environment.Exit(-1);
		}
        
        Console.WriteLine("Using driver " + drv.name);

		/* -------------------------------------------------------------------- */
		/*      Iterating through the layers                                    */
		/* -------------------------------------------------------------------- */
        Layer layer = ds.GetLayerByName(args[2]);

        if (layer == null)
        {
            Console.WriteLine("FAILURE: Couldn't fetch advertised layer " + args[2]);
            System.Environment.Exit(-1);
        }

        if (args[0].Equals("update", StringComparison.InvariantCultureIgnoreCase))
        {
            Feature feature = layer.GetFeature(long.Parse(args[3]));
            if (feature != null)
            {
                feature.SetField(args[4], args[5]);
                if (layer.SetFeature(feature) == Ogr.OGRERR_NON_EXISTING_FEATURE)
                    Console.WriteLine("feature not found");
                else
                    Console.WriteLine("feature updated successfully");
            }
            else
                Console.WriteLine("feature not found");
        }
        else if (args[0].Equals("copy", StringComparison.InvariantCultureIgnoreCase))
        {
            Feature feature = layer.GetFeature(long.Parse(args[3]));
            if (feature != null)
            {
                feature.SetFID(long.Parse(args[4]));
                if (layer.SetFeature(feature) == Ogr.OGRERR_NON_EXISTING_FEATURE)
                    Console.WriteLine("feature not found");
                else
                    Console.WriteLine("feature copied successfully");
            }
            else
                Console.WriteLine("feature not found");
        }
        else if (args[0].Equals("delete", StringComparison.InvariantCultureIgnoreCase))
        {
            if (layer.TestCapability("DeleteFeature"))
            {
                if (layer.DeleteFeature(long.Parse(args[3])) == Ogr.OGRERR_NON_EXISTING_FEATURE)
                    Console.WriteLine("feature not found");
                else
                    Console.WriteLine("feature removed successfully");
            }
            else
                Console.WriteLine("DeleteFeature not supported");
        }
        else
            Console.WriteLine("invalid command " + args[0]);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值