get lamp by id, put lamp

This commit is contained in:
Marcin Michałek committed 2015-01-25 10:26:26 +01:00
1 parent de070f90aa
commit 81bb6c2961
9 files changed
+119 -14

No files matched your search

+2
View File
@@ -21,6 +21,8 @@ public final class APIServicePaths {
//GIS
public static final String GIS_CONTROLLER = "/gis";
public static final String GET_ALL_LAMPS = "/getLamps";
public static final String PUT_LAMP = "/putLamp";
public static final String GET_LAMP = "/getLamp";
//SENSORS
public static final String SENSORS_CONTROLLER = "/sensors";
@@ -2,12 +2,14 @@ package hello.agh.edu.bazy.controllers;
import hello.APIServicePaths;
import hello.agh.edu.bazy.dbApi.gis.GisdbApi;
import hello.agh.edu.bazy.row.GisLamp;
import hello.agh.edu.bazy.model.Location;
import hello.agh.edu.bazy.row.gis.GisLamp;
import hello.agh.edu.bazy.row.gis.GisPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
/**
@@ -25,4 +27,17 @@ public class GisController {
List<GisLamp> getLamps() {
return gisdbApi.getAllLamps();
}
@RequestMapping(value = APIServicePaths.GET_LAMP + "/{id}")
@ResponseBody
List<GisPoint> getLamp(@PathVariable("id") Long id) {
return gisdbApi.getLampCoordinates(id) != null ?
gisdbApi.getLampCoordinates(id) : Collections.emptyList();
}
@RequestMapping(value = APIServicePaths.PUT_LAMP, method = RequestMethod.PUT)
@ResponseBody
int putLamp(@RequestBody Location location) {
return gisdbApi.putLamp(location.getX(), location.getY());
}
}
@@ -1,8 +1,10 @@
package hello.agh.edu.bazy.dbApi.gis;
import hello.agh.edu.bazy.postgresql.gis.GisDataSource;
import hello.agh.edu.bazy.row.GisLamp;
import hello.agh.edu.bazy.row.mappers.GisRowMapperForLamps;
import hello.agh.edu.bazy.row.gis.GisLamp;
import hello.agh.edu.bazy.row.gis.GisPoint;
import hello.agh.edu.bazy.row.mappers.gis.GisRowMapperForLamps;
import hello.agh.edu.bazy.row.mappers.gis.PointRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@@ -14,9 +16,9 @@ import java.util.List;
@Component
public class GisdbApi {
private static final String GET_ALL_LAMPS_COORDINATES = "SELECT osm_id,st_transform(way,4326) FROM gis_backup.planet_osm_point WHERE highway='street_lamp'";
private static final String PUT_LAMP = "INSERT INTO public.gis_points(gis_points_id, way, highway) VALUES (1, st_geomfromtext('POINT(? ?)',4326), 'street_lamp');";
private static final String GET_LAMP = "SELECT tags, st_transform(way,4326) FROM gis_backup.planet_osm_point WHERE osm_id=?";
private static final String GET_ALL_LAMPS_COORDINATES = "SELECT osm_id,st_transform(way,4326) FROM planet_osm_point WHERE highway='street_lamp'";
private static String PUT_LAMP = "INSERT INTO public.gis_points(gis_points_id, way, highway) VALUES (1, st_geomfromtext('POINT(@ @)',4326), 'street_lamp');";
private static final String GET_POINT_COORDINATES = "SELECT st_transform(way,900913) FROM gis_points WHERE gis_points_id = ?;";
JdbcTemplate gisJdbcTemplate;
@@ -35,7 +37,13 @@ public class GisdbApi {
return gisJdbcTemplate.query(GET_ALL_LAMPS_COORDINATES, new GisRowMapperForLamps());
}
public List<GisPoint> getLampCoordinates(long id) {
return gisJdbcTemplate.query(GET_POINT_COORDINATES, new Object[] { id }, new PointRowMapper());
}
public int putLamp(double x, double y) {
PUT_LAMP = PUT_LAMP.replaceFirst("@", Double.toString(x));
PUT_LAMP = PUT_LAMP.replaceFirst("@", Double.toString(y));
return gisJdbcTemplate.update(PUT_LAMP, x, y);
}
@@ -0,0 +1,25 @@
package hello.agh.edu.bazy.model;
/**
* Created by Marcin on 2015-01-25.
*/
public class Location {
private double x;
private double y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
@@ -1,4 +1,4 @@
package hello.agh.edu.bazy.row;
package hello.agh.edu.bazy.row.gis;
/**
* Created by mar on 24.01.15.
@@ -6,6 +6,7 @@ package hello.agh.edu.bazy.row;
public class GisLamp {
private final long id;
private final double x;
private final double y;
public double getX() {
return x;
@@ -15,8 +16,6 @@ public class GisLamp {
return y;
}
private final double y;
public GisLamp(long id, double x, double y) {
this.id = id;
this.x = x;
@@ -0,0 +1,30 @@
package hello.agh.edu.bazy.row.gis;
/**
* Created by Marcin on 2015-01-25.
*/
public class GisPoint {
private double x;
private double y;
public GisPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
@@ -1,6 +1,6 @@
package hello.agh.edu.bazy.row.mappers;
package hello.agh.edu.bazy.row.mappers.gis;
import hello.agh.edu.bazy.row.GisLamp;
import hello.agh.edu.bazy.row.gis.GisLamp;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.springframework.jdbc.core.RowMapper;
@@ -14,6 +14,7 @@ public class GisRowMapperForLamps implements RowMapper<GisLamp> {
private static final String COORDINATE_COLUM_NNAME = "st_transform";
private static final String OSM_ID = "osm_id";
@Override
public GisLamp mapRow(ResultSet rs, int rowNum) throws SQLException {
PGgeometry geometry = (PGgeometry)rs.getObject(COORDINATE_COLUM_NNAME);
@@ -0,0 +1,25 @@
package hello.agh.edu.bazy.row.mappers.gis;
import hello.agh.edu.bazy.row.gis.GisPoint;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Created by mar on 22.01.15.
*/
public class PointRowMapper implements RowMapper<GisPoint> {
private static final String ST_AS_TEXT = "st_transform";
@Override
public GisPoint mapRow(ResultSet rs, int rowNum) throws SQLException {
PGgeometry geometry = (PGgeometry)rs.getObject(ST_AS_TEXT);
return new GisPoint(
((Point) geometry.getGeometry()).getX(),
((Point) geometry.getGeometry()).getY());
}
}
@@ -1,6 +1,6 @@
package hello.agh.edu.bazy.dbApi.gis;
import hello.agh.edu.bazy.row.GisLamp;
import hello.agh.edu.bazy.row.gis.GisLamp;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;