3

I am working with PostgreSQL 9.4 and Java EE. I want to insert shapefile or layer's features into PostgreSQL, is there any solution so I can insert the whole shapefile into the database? OS is Windows 10.

I have successfully inserted one point into my database (PostgreSQL) table but I want all the features like: polygon, multi-polygon, marker etc.

Whatever I draw on the map should be stored into particular table of DB. I am getting all the features but unable to store them. How I can do this?

    try
    {   Class.forName("org.postgresql.Driver");
        conn=DriverManager.getConnection(url,username, password);
        sql=conn.createStatement();
        result1 = "insert into test(feature) values(ST_GeomFromText("+data+"))";
        result1 = "insert into test(feature,lat,lan) values(ST_GeomFromText('"+feature+"),"+lat+","+lan+"')";
        result1 = "insert into test(feature) values(ST_GeomFromText('POINT("+lat+" "+lan+")', 4326))";
        result1 = "insert into test(feature) values(ST_GeomFromText('"+feature+"("+lat+" "+lan+")'))";
        sql.executeUpdate(result1);
        out.println("data inserted...");

        String nextJSP = "/index.jsp";
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
        dispatcher.forward(request,response);

        out.println("forwarding...");
        //System.out.println(fst);
    }   
    catch(SQLException | ClassNotFoundException e)
    {
        e.printStackTrace();
    }
}

enter image description here

Getting table data and project it on map:

<div id="map"></div>
<%
JSONObject GJson = (JSONObject) request.getAttribute("geoJson");
//String GJson = (String)request.getAttribute("geoJson");
System.out.println("data at jsp : "+GJson);
%> 
<script type="text/javascript">
var geojson = <%= GJson%>
console.log(JSON.stringify(geojson));
var map = L.map('map');
//var geoJsonData = GeoJSON.parse(json);
var jsonLayer = L.geoJson().addTo(map);
console.log(map.hasLayer(jsonLayer));
jsonLayer.addData(geojson); 
</script>
  • Please edit the question to clarify the meaning of "insert the whole shapefile" (do you mean as a BLOB or the features of the shapefile), Your definition of "layer" is also unclear. Note that coding questions here at GIS SE are expected to contain code. – Vince Mar 31 '16 at 13:15
  • Is your use case different from using the shp2pgsql command line tool? – til_b Mar 31 '16 at 13:18
  • The command line for laoding the shapefile is: shp2pgsql -I -s <SRID> <PATH/TO/SHAPEFILE> <SCHEMA>.<DBTABLE> | psql -U postgres -d <DBNAME> The task is same but i have to do this using java code @til_b – Gary Apr 1 '16 at 4:50
  • Does calling the shp2pgsql command from java (using java.io.Runtime.getRuntime().exec(...) or similar) help you? – til_b Apr 1 '16 at 19:26
  • @til_b I edited the code in my question hope it will clear my need. right now I'm successfully getting the shapefile data in Geojson format and that data i have to put into DB(postgresql 9.4). – Gary Apr 2 '16 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.