This commit is contained in:
Marcin Michałek committed 2015-01-27 23:12:38 +01:00
commit 9690f47213
7 files changed
+78 -25

No files matched your search

@@ -4,6 +4,7 @@ import hello.agh.edu.bazy.dbApi.gis.GisDbApi;
import hello.agh.edu.bazy.dbApi.object.ObjectDbApi;
import hello.agh.edu.bazy.model.Location;
import hello.agh.edu.bazy.model.Point;
import hello.agh.edu.bazy.model.Polygon;
import hello.agh.edu.bazy.path.APIServicePaths;
import hello.agh.edu.bazy.row.gis.GisPoint;
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,15 +42,17 @@ public class GisController {
@RequestMapping(value = APIServicePaths.GET_LAMP + "/{id}")
@ResponseBody
Location getLamp(@PathVariable("id") Long id) {
Location getLamp(@PathVariable("id") Long id) throws Exception {
Location location = objectDbApi.getObject(id);
GisPoint points = gisDbApi.getLamp(id).get(0);
location.setY(points.getY());
location.setX(points.getX());
List<GisPoint> points = gisDbApi.getLamp(id);
if (points.size() == 0) throw new Exception("No lapm found id: " + id);
GisPoint p = points.get(0);
location.setY(p.getY());
location.setX(p.getX());
return location;
}
@RequestMapping(value = APIServicePaths.GET_POLYGON + "{id}", method = RequestMethod.PUT)
@RequestMapping(value = APIServicePaths.GET_POLYGON + "/{id}")
@ResponseBody
List<Point> getPolygon(@PathVariable("id") long id) {
return gisDbApi.getPolygon(id);
@@ -57,7 +60,7 @@ public class GisController {
@RequestMapping(value = APIServicePaths.GET_POLYGON)
@ResponseBody
List<List<Point>> getPolygons() {
List<Polygon> getPolygons() {
return gisDbApi.getPolygons();
}
@@ -71,8 +74,13 @@ public class GisController {
@RequestMapping(value = APIServicePaths.DELETE_LAMP + "/{id}", method = RequestMethod.DELETE)
@ResponseBody
int deleteLamp(@PathVariable("id") Long id) {
int deletePoint(@PathVariable("id") Long id) {
return gisDbApi.deleteLamp(id);
}
@RequestMapping(value = APIServicePaths.DELETE_POLYGON + "/{id}", method = RequestMethod.DELETE)
@ResponseBody
int deleteLamp(@PathVariable("id") Long id) {
return objectDbApi.deleteObject(id);
}
}
@@ -1,6 +1,7 @@
package hello.agh.edu.bazy.dbApi.gis;
import hello.agh.edu.bazy.model.Point;
import hello.agh.edu.bazy.model.Polygon;
import hello.agh.edu.bazy.postgresql.gis.GisDataSource;
import hello.agh.edu.bazy.row.gis.GisPoint;
import hello.agh.edu.bazy.row.mappers.gis.PointRowMapper;
@@ -21,7 +22,7 @@ public class GisDbApi {
private static String PUT_LAMP = "INSERT INTO public.gis_points(gis_points_id, way, highway) VALUES (@, st_geomfromtext('POINT(@ @)',4326), 'street_lamp');";
private static final String GET_LAMP = "SELECT gis_points_id,st_transform(way,4326) FROM gis_points WHERE gis_points_id = ?;";
private static final String DELETE_LAMP = "DELETE FROM public.gis_points WHERE gis_points_id = ?";
private static final String GET_POLYGONS = "SELECT st_transform(way,4326) as polygon FROM gis_polygons;";
private static final String GET_POLYGONS = "SELECT gis_polygons_id,st_transform(way,4326) as polygon FROM gis_polygons;";
private static final String GET_POLYGON = "SELECT st_transform(way,4326) as polygon FROM gis_polygons WHERE gis_polygons_id = ?";
private static final String PUT_POLYGON = "INSERT INTO public.gis_polygons(gis_polygons_id, way, highway) VALUES (?, (SELECT ST_MakePolygon(ST_AddPoint(foo.open_line, ST_StartPoint(foo.open_line))) ";
@@ -59,7 +60,7 @@ public class GisDbApi {
return gisJdbcTemplate.update(sql);
}
public List<List<Point>> getPolygons(){
public List<Polygon> getPolygons(){
return gisJdbcTemplate.query(GET_POLYGONS, new PolygonsRowMapper());
}
@@ -18,14 +18,15 @@ import java.util.List;
* Created by mar on 27.01.15.
*/
@Component
public class ObjectDbApi {
public class ObjectDbApi {
private static final String LOCATION_ID = "location_id";
private static final String PUT_OBJECT = "insert into \"LOCALIZATIONS\" values (DEFAULT,?)";
private static final String GET_LOCATION = "select s.sensor_id, l.* from \"SENSORS_LOCATION\" s JOIN \"LOCALIZATIONS\" l ON s.location_id = l.location_id where s.location_id = ? ";
private static final String GET_LOCATION = "select * from \"LOCALIZATIONS\" where location_id = ?";
private static final String DELETE_LOCATION = "delete from \"LOCALIZATIONS\" where location_id = ?";
JdbcTemplate objectJdbcTemplate;
KeyHolder keyHolder = new GeneratedKeyHolder();
public ObjectDbApi(){
public ObjectDbApi() {
GisDataSource dataSource = new GisDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://10.156.207.136:5432/objectsdb");
@@ -36,20 +37,23 @@ public class ObjectDbApi {
objectJdbcTemplate = template;
}
public long putObject(final String type){
public long putObject(final String type) {
objectJdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement(PUT_OBJECT,new String[]{LOCATION_ID});
ps.setString(1,type);
PreparedStatement ps = con.prepareStatement(PUT_OBJECT, new String[]{LOCATION_ID});
ps.setString(1, type);
return ps;
}
},keyHolder);
}, keyHolder);
return keyHolder.getKey().longValue();
}
public Location getObject(long id){
return objectJdbcTemplate.query(GET_LOCATION,new Object[]{id}, new PointRowMapper()).get(0);
public Location getObject(long id) {
return objectJdbcTemplate.query(GET_LOCATION, new Object[]{id}, new PointRowMapper()).get(0);
}
}
public int deleteObject(long id){
return objectJdbcTemplate.update(DELETE_LOCATION,new Object[]{id});
}
}
@@ -19,4 +19,12 @@ public class Point {
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
@@ -0,0 +1,27 @@
package hello.agh.edu.bazy.model;
import java.util.List;
/**
* Created by mar on 27.01.15.
*/
public class Polygon {
private long id;
private List<Point> polygon;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<Point> getPolygon() {
return polygon;
}
public void setPolygon(List<Point> polygon) {
this.polygon = polygon;
}
}
@@ -10,6 +10,7 @@ package hello.agh.edu.bazy.path;
public final class APIServicePaths {
;
private APIServicePaths() {
}
@@ -27,7 +28,8 @@ public final class APIServicePaths {
public static final String PUT_LAMP = "/lamp";
public static final String DELETE_LAMP = "/lamp";
public static final String GET_LAMP = "/lamp";
public static final String GET_POLYGON = "polygon";
public static final String GET_POLYGON = "/polygon";
public static final String DELETE_POLYGON = "/polygon";
@@ -1,9 +1,9 @@
package hello.agh.edu.bazy.row.mappers.gis;
import hello.agh.edu.bazy.model.Point;
import hello.agh.edu.bazy.model.Polygon;
import hello.agh.edu.bazy.row.gis.GisPoint;
import org.postgis.PGgeometry;
import org.postgis.Polygon;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
@@ -14,10 +14,9 @@ import java.util.List;
/**
* Created by lukasgol on 26.01.15.
*/
public class PolygonsRowMapper implements RowMapper<List<Point>> {
public class PolygonsRowMapper implements RowMapper<Polygon>{
@Override
public List<Point> mapRow(ResultSet rs, int rowNum) throws SQLException {
public Polygon mapRow(ResultSet rs, int rowNum) throws SQLException {
PGgeometry geometry = (PGgeometry) rs.getObject("polygon");
int size = geometry.getGeometry().numPoints();
List<Point> list = new ArrayList<>();
@@ -25,6 +24,10 @@ public class PolygonsRowMapper implements RowMapper<List<Point>> {
Point p = new Point(geometry.getGeometry().getPoint(i).x,geometry.getGeometry().getPoint(i).y);
list.add(p);
}
return list;
Polygon p = new Polygon();
p.setId(rs.getLong("gis_polygons_id"));
p.setPolygon(list);
return p;
}
}