0

I'm tyring to convert a shapefile to MapInfo using ogr2ogr. (C#/.NET 6)

I get the following error when running the code below: Unable to open datasource [directory path]\file.mif.

However, If I open cmd.exe in the ogr2ogr directory and copy/paste the argument, it executes and the shapefile is converted to .mif/.mid, as I would expect it to.

command line argument = ogr2ogr -f "MapInfo File" -overwrite -dsco FORMAT=MIF "[directory path]\Polygon.mif" "[directory path]\Polygon.shp"

Code:

var ogr2ogrPath = "[directory path]\\ogr2ogr.exe"

ProcessStartInfo StartInfo = new ProcessStartInfo() {
  FileName = ogr2ogrPath,
  CreateNoWindow = true,
  UseShellExecute = false,
  RedirectStandardOutput = true,
  RedirectStandardError = true,
  Arguments = $"ogr2ogr -f \"MapInfo File\" -overwrite -dsco FORMAT=MIF \"[directory path]\file.mif\" \"[directory path]\file.shp\"",
};

var streamOutput = "";

using(var shellProcess = Process.Start(StartInfo)) {
  StreamReader readerStdOut = shellProcess.StandardError;

  while (readerStdOut.EndOfStream == false) {
    streamOutput += readerStdOut.ReadLine() + Environment.NewLine;
  }

  shellProcess.WaitForExit();
  streamOutput += " - Finished " + Environment.NewLine;
}
2
  • Possibly due to the backslashes in the file path, try replacing them with double backslashes, i.e. C:\\example\\test.shp
    – mikewatt
    Apr 6, 2022 at 20:22
  • Thanks for the suggestion. The paths already have serialized backslashes/ double backslashes, so that is not the issue. Apr 7, 2022 at 8:06

1 Answer 1

0

This was the solution, for anyone experiencing the same issue:

ProcessStartInfo StartInfo = new ProcessStartInfo() {
  FileName = "cmd.exe",
  CreateNoWindow = true,
  UseShellExecute = false,
  RedirectStandardOutput = true,
  RedirectStandardError = true,
  WorkingDirectory = [Path to ogr2ogr directory],
  Arguments = $"ogr2ogr -f \"MapInfo File\" -overwrite -dsco FORMAT=MIF \"[directory path]\file.mif\" \"[directory path]\file.shp\"",
  Verb = "runas"
};

var streamOutput = "";

using(var shellProcess = Process.Start(StartInfo)) {
  StreamReader readerStdOut = shellProcess.StandardError;

  while (readerStdOut.EndOfStream == false) {
    streamOutput += readerStdOut.ReadLine() + Environment.NewLine;
  }

  shellProcess.WaitForExit();
  streamOutput += " - Finished " + Environment.NewLine;
}

Your Answer

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

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