7

I have an ogr2ogr (GDAL) command that I run to export points from a database to an XML file.

ogr2ogr-f GPX c:\temp\points.gpx PG:"host=localhost port=5432
dbname=SpatialPlayground schemas=public user=postgres password=password"
-sql "SELECT * FROM PointsForGpxExport"

How do I run this from a C# console application and get the resulting file (or a stream) so I can perform additional processing?

One thought is to start a new process from within the console app then grab the exported file and continue processing. Is this the best approach?

2 Answers 2

1

You can run through CMD in silent mode, and get results from CMD, check below code:

 Process process = new Process();
 process.StartInfo.FileName = "cmd.exe";
 process.StartInfo.UseShellExecute = false;
 process.StartInfo.CreateNoWindow = true;
 process.StartInfo.Arguments = @"ogr2ogr-f GPX c:\temp\points.gpx PG:\"host=localhost port=5432
dbname=SpatialPlayground schemas=public user=postgres password=password\"
-sql \"SELECT * FROM PointsForGpxExport\"" corextbranch=surfacert_v2_blue_kit&c:\depot\tools\path1st\myenv.cmd";

 process.StartInfo.RedirectStandardOutput = true;
 process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            // Prepend line numbers to each line of the output.
            if (!String.IsNullOrEmpty(e.Data))
            {
                lineCount++;
                output.Append("\n[" + lineCount + "]: " + e.Data);
            }
        });

 process.Start();

 // Asynchronously read the standard output of the spawned process. 
 // This raises OutputDataReceived events for each line of output.
 process.BeginOutputReadLine();
 process.WaitForExit();

 // Write the redirected output to this application's window.
 Console.WriteLine(output);

 process.WaitForExit();
 process.Close();
-1

That is one way to do it. How much time / effort do you want to put into it?

With a GDAL distribution, you have the code for ogr2ogr. you could peruse that code and figure out how it does the above command, and then add your processing to that C/C++ utility, thus generating your own. you could replicate that processing in C# by calling the .NET bindings and do it all in one process. the downside is that you'd have to put in the time / effort to write a program. the upside is you'd have a tool that is easy(ish) to distribute if anyone else needed to use it.

1
  • You are just stating the obvious. Would appreciate if you can give more content on how to actually achieving what you state
    – Graviton
    Commented Dec 27, 2018 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.