地理几何元素的增删改分为两种方式,一种是在后端对数据库进行操作,另一种是在前端对feature要素进行修改(具体形式跟前端的技术有关,比如Leaflet、Openlayer)。
本次主要介绍的是通过后端实现对postgis数据库中地理几何元素的增删改,通过前端修改的方式将在后期的博文中介绍。
采用后端进行postgis中地理几何元素的增删改优点是减轻前端的压力。
org.postgresql
postgresql
runtime
jdbcUrl: jdbc:postgresql://localhost:5432/test?useSSL=false
username: postgres
password: 123456
driverClassName: org.postgresql.Driver
SELECT nextval('map_elements_id_seq'::regclass)
insert into map_elements(name, longitude, latitude, element_location)
values (#{name}, #{longitude}, #{latitude}, ST_GeomFromText(#{geoStr}, 4326))
delete from map_elements where id = #{id}
UPDATE map_elements
name=#{name},
longitude=#{longitude},
latitude=#{latitude},
element_location=ST_GeomFromText(#{geoStr}, 4326),
WHERE id=#{id}
package com.ol.openlayer.project.feature.mapper;
import com.ol.openlayer.project.feature.domain.MapElement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* (GiPayApply)表数据库访问层
*
* @author makejava
* @since 2020-04-14 16:16:15
*/
@Mapper
public interface MapMapper {
/** * @param mapElement */
void addMapElement(MapElement mapElement);
/** * @param id id * @return 依据id返回数据 */
MapElement findById(Long id);
void deleteMapElement(Long id);
void updateMapElement(MapElement mapElement);
}
package com.ol.openlayer.project.feature.service.impl;
import com.ol.openlayer.project.feature.domain.MapElement;
import com.ol.openlayer.project.feature.mapper.MapMapper;
import com.ol.openlayer.project.feature.service.MapService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* (GiPayApply)表服务实现类
*
* @author makejava
* @since 2020-04-14 16:16:15
*/
@Service("mapService")
public class MapServiceImpl implements MapService {
@Resource
private MapMapper mapMapper;
@Override
public void addMapElement(MapElement mapElement) {
mapMapper.addMapElement(mapElement);
}
@Override
public MapElement findById(Long id) {
return mapMapper.findById(id);
}
@Override
public void deleteMapElement(Long id){
mapMapper.deleteMapElement(id);
}
@Override
public void updateMapElement(MapElement mapElement)
{
mapMapper.updateMapElement(mapElement);
}
}
package com.ol.openlayer.project.feature.controller;
import com.ol.openlayer.project.feature.domain.MapElement;
import com.ol.openlayer.project.feature.service.MapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import static com.ol.openlayer.common.utils.map.Geometry.geometryToString;
@Api(tags = "空间数据操作")
@RestController
public class FeatureController {
@Resource
private MapService mapService;
/**
* * 添加地图元素 *
* @param mapElement
* * @return 添加的地理元素信息
* */
@ApiOperation(value = "上传矢量")
@PostMapping("/feature/add")
public MapElement addMapElement(@RequestBody MapElement mapElement){
mapElement.setGeoStr(geometryToString(mapElement.getLongitude(), mapElement.getLatitude()));
mapService.addMapElement(mapElement);
Long id = mapElement.getId();
return mapService.findById(id);
}
/** *
* @param id id *
* @return 是否删除成功 */
@ApiOperation(value = "删除矢量")
@DeleteMapping("/feature/delete/{id}")
public Boolean deleteMapElement(@PathVariable Long id){
Boolean deleteMapElementSuccess = true;
try{
mapService.deleteMapElement(id);
}catch (Exception e){
// log.info("删除失败:" + e);
deleteMapElementSuccess = false;
}
return deleteMapElementSuccess;
}
/** * 数据更改 *
* @param mapElement *
* @return 更改后的数据 */
@ApiOperation(value = "编辑矢量")
@PutMapping("/feature/modify")
public MapElement updateMapElement(@RequestBody MapElement mapElement){
mapElement.setGeoStr(geometryToString(mapElement.getLongitude(), mapElement.getLatitude()));
mapService.updateMapElement(mapElement);
Long id = mapElement.getId();
return mapService.findById(id);
}
}
关注以下公众号,关注各种技术文章,获取本系列后续推送与分享