[gdal-dev] ogr2ogr C#
Randy George
rkgeorge at cadmaps.com
Fri Mar 27 10:43:34 EDT 2009
Or do something ugly like use a .asmx to call your ogr2ogr cmdline as a
Process:
using System;
using System.Web.Services;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Security.Permissions;
[WebService(Namespace = "http://www.myserver.com/ShpView")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Unrestricted =
true)]
public class RunTranslate : System.Web.Services.WebService {
private static string basepath = "E:/ShpView/";
private static string pass = "pass";
private static string db = "DBname";
public RunTranslate () {
}
private delegate string StringDelegate();
[WebMethod]
public string PostGISLoadCommand(string shp, string epsg)
{
string fileName = "E:/ShpView/ogr/bin/ogr2ogr";
string arguments = "-f PostgreSQL PG:\"user=user dbname="+db+"
host=localhost password="+pass+" port=5432\" " + basepath +
"/shpview/data/shape/" + shp + ".shp -a_srs \"" + epsg + "\" -overwrite";
return Run(fileName, arguments);
}
private static string Run(string fileName, string arguments)
{
string results = "";
string error = "";
Process cmdLineProcess = new Process();
using (cmdLineProcess)
{
cmdLineProcess.StartInfo.FileName = fileName;
cmdLineProcess.StartInfo.Arguments = arguments;
cmdLineProcess.StartInfo.UseShellExecute = false;
cmdLineProcess.StartInfo.CreateNoWindow = true;
cmdLineProcess.StartInfo.RedirectStandardOutput = true;
cmdLineProcess.StartInfo.RedirectStandardError = true;
if (cmdLineProcess.Start())
{
StringDelegate outputStreamAsyncReader = new
StringDelegate(cmdLineProcess.StandardOutput.ReadToEnd);
StringDelegate errorStreamAsyncReader = new
StringDelegate(cmdLineProcess.StandardError.ReadToEnd);
IAsyncResult outAR =
outputStreamAsyncReader.BeginInvoke(null, null);
IAsyncResult errAR =
errorStreamAsyncReader.BeginInvoke(null, null);
if (Thread.CurrentThread.GetApartmentState() ==
ApartmentState.STA)
{
while (!(outAR.IsCompleted && errAR.IsCompleted))
{
Thread.Sleep(10);
}
}
else
{
WaitHandle[] arWaitHandles = new WaitHandle[2];
arWaitHandles[0] = outAR.AsyncWaitHandle;
arWaitHandles[1] = errAR.AsyncWaitHandle;
if (!WaitHandle.WaitAll(arWaitHandles))
{
error = String.Format("Command line aborted: {0}",
fileName);
}
}
results = outputStreamAsyncReader.EndInvoke(outAR);
error = errorStreamAsyncReader.EndInvoke(errAR);
if (!cmdLineProcess.HasExited)
{
cmdLineProcess.WaitForExit();
}
}
else
{
error = String.Format("Could not start command line process:
{0}", fileName);
}
cmdLineProcess.Close();
}
return "Done: " + error + " " + results;
}
}
From: gdal-dev-bounces at lists.osgeo.org
[mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of Tamas Szekeres
Sent: Friday, March 27, 2009 3:39 AM
To: Sune Dogan, LIFA A/S
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] ogr2ogr C#
Hi,
You'll have to re-implement the logic in ogr2ogr.cpp by using the
ogr_csharp.dll.
Refer to the following thread for more information:
http://thread.gmane.org/gmane.comp.gis.gdal.devel/11789
Best regards,
Tamas
2009/3/27 Sune Dogan, LIFA A/S <sud at lifa.dk>
Hi
Im developing a .NET C# project and need to convert from GML to ESRI Shape
Simply i want to run this command:
ogr2ogr -skipfailures -f "ESRI Shapefile" C:\shapefile.shp c:\gmlfile.gml
which works fine from the FWTool Command tool.
But i just want to do i in my application, can any one help me?
Thanks :)
_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20090327/ca05c143/attachment-0001.html
More information about the gdal-dev
mailing list