I'm working on a geographic information system project, whose products form shapefiles. I am using the Java programming language and a PostGIS database. Is there a way to generate a shapefile from PostGIS or KML using the Java language?
4 Answers
Geotools is a powerful library, and can read from PostGIS, for example, and can export to ShapeFile, for example, using the "Feature" object.
I cann't show you a complete "example", but it's easy to find out a way to do it, in Google.
You can use geotools to do such actions. You may find here an example converting a java ResultSet to shapefile. Though the example provided is using an oracle ResultSet but you can easily adjust it to accept postgis ResultSet and generate shapefiles.
H2GIS is a lightweight (about 5 megabytes) embedded spatial database written in Java and available on Maven Central. It could suit your needs. A short sample of using this library is available.
generate a shp file from PostGIS
H2GIS can import/export SHP files. You can use the SHPDriverFunction by providing a JDBC connection with PostGIS.You should use this connection wrapper in order to bind JTS geometry object to postgis geometry object.
generate a kml file from PostGIS
A KMLDriverFunction is also available.
Please note that the H2GIS library is under the GPLV3 license. In order to use this your software must be open-source.