0

How can I import gis shapefiles into postgis database programatically using java ? I used a program named postgis shapefile import but now I want to do it in my java code.

1
  • 1
    osm2pgsql is a command line tool for that, maybe doing a batch execution of this via bash is an option for you? Commented Mar 18, 2014 at 15:55

1 Answer 1

2

The most common tool (one of several) is shp2pgsql, which comes with PostGIS. It is normally used from a command prompt, but can be used through Java's ProcessBuilder.

ProcessBuilder pb =
   new ProcessBuilder("/bin/sh", "-c", "shp2pgsql my.shp | psql -d mydb");
Process p = pb.start();

ogr2ogr is another commonly used tool to convert most geospatial vector formats to PostGIS.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer, shp2pgsql has many options when we work with its gui. How can I pass this parameters in command? I searched all available parameters but I can't find any of them inside those parameters.
@Obtice the gui does the same as the command, which is well documented
thanks a lot, could you please explain what "/bin/sh" and "-c" is ?
It depends on what OS you have. If you have Linux or Mac OS X, this process the command that has a pipe | character with /bin/sh (i.e., see man sh). But if you have a different OS, you will need to set up the ProcessBuilder differently.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.