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 Answer
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.
4 Comments
Obtice
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.
Mike T
@Obtice the gui does the same as the command, which is well documented
Obtice
thanks a lot, could you please explain what "/bin/sh" and "-c" is ?
Mike T
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.