1

I want to use the command gdal_polygonize image.png bla in a Java for-loop, where image.png and bla changes in every iteration. How can I do this?

I searched for it on google and stackexchange and I only found a similiar question for Python (but it is not quite answered I think).

Any suggestions? Maybe a Batch script which I could run in every iteration?

2
  • As answered previously, use the Java API.
    – Mike T
    Commented Nov 3, 2014 at 11:34
  • Thanks but it took me a long time to get it work with osgeo4w and now I want to keep it :D Anyway I found another way. (see answer below). Again thank you very much for your answer.
    – Selphiron
    Commented Nov 3, 2014 at 13:55

1 Answer 1

1

I came across this It is very simple. Example: for gdal_polygonize hello.png bla you can use:

    import java.io.IOException;
    import org.apache.commons.exec.CommandLine;
    import org.apache.commons.exec.DefaultExecutor;
    import org.apache.commons.exec.ExecuteException;
    public class Test {

        public static void main(String[] args) throws ExecuteException, IOException {

             String line = "C:\\OSGeo4W64\\OSGeo4W.bat";
             CommandLine commandLine = CommandLine.parse(line);
             commandLine.addArgument("gdal_polygonize");
             commandLine.addArgument("hello.png"); //Use path to image if necessary
             commandLine.addArgument("bla"); //Use path if necessary 
             DefaultExecutor executor = new DefaultExecutor();
             executor.execute(commandLine);

         }
   }

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.