初始化

This commit is contained in:
hukekuan@163.com 2018-03-12 11:27:28 +08:00
parent 7670fd8d4f
commit 8a450864b8
5 changed files with 105 additions and 26 deletions

View File

@ -6,6 +6,8 @@ package com.gis3c.common.exception;
public class BusinessException extends RuntimeException { public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Integer exceptionCode;
public BusinessException(){ public BusinessException(){
super(); super();
} }
@ -21,4 +23,24 @@ public class BusinessException extends RuntimeException {
public BusinessException(String message, Throwable cause) { public BusinessException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public BusinessException(Integer exceptionCode){
super();
this.exceptionCode = exceptionCode;
}
public BusinessException(String message,Integer exceptionCode) {
super(message);
this.exceptionCode = exceptionCode;
}
public BusinessException(Throwable cause,Integer exceptionCode) {
super(cause);
this.exceptionCode = exceptionCode;
}
public BusinessException(String message, Throwable cause,Integer exceptionCode) {
super(message, cause);
this.exceptionCode = exceptionCode;
}
} }

View File

@ -2,6 +2,7 @@ package com.gis3c.spatial.common;
import com.gis3c.spatial.entity.BaseFeature; import com.gis3c.spatial.entity.BaseFeature;
import com.gis3c.spatial.entity.Region;
import org.geotools.data.DataUtilities; import org.geotools.data.DataUtilities;
import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.geojson.feature.FeatureJSON; import org.geotools.geojson.feature.FeatureJSON;
@ -18,6 +19,34 @@ import java.util.List;
* Created by hukekuan on 2018/1/16. * Created by hukekuan on 2018/1/16.
*/ */
public class FeatureUtilities { public class FeatureUtilities {
/**
* SimpleFeature转GeoJSON字符串
* @param simpleFeature
* @return
*/
public static String Feature2Json(SimpleFeature simpleFeature) throws IOException {
String result = null;
FeatureJSON fjson = new FeatureJSON();
StringWriter writer = new StringWriter();
fjson.writeFeature(simpleFeature,writer);
result = writer.toString();
return result;
}
/**
*
* @param t 继承于BaseFeature的实体类转换成GeoJSON字符串
* @param featureIndex
* @param <T>
* @return
* @throws IOException
*/
public static <T extends BaseFeature> String JavaBean2Json(T t,String featureIndex)
throws IOException {
SimpleFeature simpleFeature =t.javaBean2SimpleFeature("featureIndex");
return Feature2Json(simpleFeature);
}
/** /**
* @Description: 将java bean转换为Simple Feature * @Description: 将java bean转换为Simple Feature
* @param beansList * @param beansList
@ -30,9 +59,8 @@ public class FeatureUtilities {
throw new NullPointerException("参数无效"); throw new NullPointerException("参数无效");
} }
List<SimpleFeature> simpleFeatureList = new ArrayList<>(); List<SimpleFeature> simpleFeatureList = new ArrayList<>();
SimpleFeatureType featureType = beansList.get(0).createFeatureType();
for(int i = 0,len = beansList.size();i < len;i++){ for(int i = 0,len = beansList.size();i < len;i++){
simpleFeatureList.add(beansList.get(i).javaBean2SimpleFeature(featureType,Integer.toString(i))); simpleFeatureList.add(beansList.get(i).javaBean2SimpleFeature(Integer.toString(i)));
} }
return simpleFeatureList; return simpleFeatureList;

View File

@ -1,5 +1,6 @@
package com.gis3c.spatial.entity; package com.gis3c.spatial.entity;
import com.gis3c.common.exception.BusinessException;
import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;
import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
@ -55,11 +56,11 @@ public class BaseFeature {
* @throws NullPointerException * @throws NullPointerException
* @date 2017-07-10 下午6:39 * @date 2017-07-10 下午6:39
*/ */
public SimpleFeatureType createFeatureType() throws NullPointerException{ public SimpleFeatureType createFeatureType() {
SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder(); SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder();
SimpleFeatureType featureType=null; SimpleFeatureType featureType=null;
if(!geometryChecked()){ if(!geometryChecked()){
throw new NullPointerException("空间字段为空"); throw new BusinessException("空间字段为空");
} }
CoordinateReferenceSystem crs = null; CoordinateReferenceSystem crs = null;
@ -77,28 +78,28 @@ public class BaseFeature {
/** /**
* @author hukekuan * @author hukekuan
* @Description 生成SimpleFeature对象 * @Description 生成SimpleFeature对象
* @param featureType,转换类对应的featureType
* @param featureIndex,feature序列号 * @param featureIndex,feature序列号
* @return 对象转SimpleFeature * @return 对象转SimpleFeature
* @throws IllegalArgumentException * @throws IllegalArgumentException
* @throws IllegalAccessException * @throws IllegalAccessException
* @date 2017-07-10 下午7:10 * @date 2017-07-10 下午7:10
*/ */
public SimpleFeature javaBean2SimpleFeature(SimpleFeatureType featureType, String featureIndex) public SimpleFeature javaBean2SimpleFeature(String featureIndex) {
throws IllegalArgumentException, IllegalAccessException { SimpleFeatureType featureType = this.createFeatureType();
SimpleFeature simpleFeature = null;
if(featureType == null){
throw new IllegalArgumentException("featureType参数为空");
}
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
List<Field> fields = this.AllFieldes(); List<Field> fields = this.AllFieldes();
List<Object> objList = new ArrayList<>(); List<Object> objList = new ArrayList<>();
for (Field field : fields) { try {
field.setAccessible(true); for (Field field : fields) {
objList.add(field.get(this)); field.setAccessible(true);
objList.add(field.get(this));
}
} catch (IllegalAccessException e) {
throw new BusinessException("无访问权限");
} }
simpleFeature = featureBuilder.buildFeature(featureIndex, objList.toArray());
return simpleFeature; return featureBuilder.buildFeature(featureIndex, objList.toArray());
} }

View File

@ -14,7 +14,7 @@ public interface RegionService {
* 获取区域实体 * 获取区域实体
* @param reginCode * @param reginCode
*/ */
public Region findRegionByCode(String reginCode); public String findRegionByCode(String reginCode);
/** /**
* 获取区域中心点 * 获取区域中心点
@ -28,14 +28,14 @@ public interface RegionService {
* @param reginCode * @param reginCode
* @return * @return
*/ */
public List<Region> findAroundRegions(String reginCode); public String findAroundRegions(String reginCode);
/** /**
* 获取区域内部的所有子区域 * 获取区域内部的所有子区域
* @param parentCode * @param parentCode
* @return * @return
*/ */
public List<Region> findRegionsByParentCode(String parentCode); public String findRegionsByParentCode(String parentCode);
/** /**
* 获取子区域内部的所有子区域的中心点 * 获取子区域内部的所有子区域的中心点

View File

@ -1,13 +1,18 @@
package com.gis3c.spatial.service.impl; package com.gis3c.spatial.service.impl;
import com.gis3c.common.exception.BusinessException;
import com.gis3c.spatial.common.FeatureUtilities;
import com.gis3c.spatial.dao.RegionDao; import com.gis3c.spatial.dao.RegionDao;
import com.gis3c.spatial.entity.Region; import com.gis3c.spatial.entity.Region;
import com.gis3c.spatial.entity.RegionCenter; import com.gis3c.spatial.entity.RegionCenter;
import com.gis3c.spatial.entity.RegionType; import com.gis3c.spatial.entity.RegionType;
import com.gis3c.spatial.service.RegionService; import com.gis3c.spatial.service.RegionService;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,11 +25,18 @@ public class RegionServiceImpl implements RegionService {
private RegionDao regionDao; private RegionDao regionDao;
@Override @Override
public Region findRegionByCode(String reginCode) { public String findRegionByCode(String reginCode) {
return regionDao.findRegionByCode( String regionJson = null;
Region queryRegion = regionDao.findRegionByCode(
regionTableByCode(reginCode).getName(), regionTableByCode(reginCode).getName(),
reginCode reginCode
); );
try {
regionJson = FeatureUtilities.JavaBean2Json(queryRegion,"1");
} catch (IOException e) {
throw new BusinessException("Region实体转GeoJSON出错");
}
return regionJson;
} }
@Override @Override
@ -36,25 +48,41 @@ public class RegionServiceImpl implements RegionService {
} }
@Override @Override
public List<Region> findAroundRegions(String reginCode) { public String findAroundRegions(String reginCode) {
return regionDao.findAroundRegions( String result = null;
List<Region> regionLsit = regionDao.findAroundRegions(
regionTableByCode(reginCode).getName(), regionTableByCode(reginCode).getName(),
reginCode reginCode
); );
try {
result = FeatureUtilities.JavaBeans2Json(regionLsit);
} catch (IllegalAccessException | IOException e) {
throw new BusinessException("Region实体转GeoJSON出错");
}
return result;
} }
@Override @Override
public List<Region> findRegionsByParentCode(String parentCode) { public String findRegionsByParentCode(String parentCode) {
String result = null;
RegionType parentType = regionTableByCode(parentCode); RegionType parentType = regionTableByCode(parentCode);
RegionType regionType = parentType.ChildType(); RegionType regionType = parentType.ChildType();
if (regionType == null) { if (regionType == null) {
throw new IllegalArgumentException("新政区编号输入错误"); throw new BusinessException("新政区编号输入错误");
} }
return regionDao.findRegionsByParentCode( List<Region> regionLsit = regionDao.findRegionsByParentCode(
regionType.getName(), regionType.getName(),
getlikeCode(parentCode) getlikeCode(parentCode)
); );
try {
result = FeatureUtilities.JavaBeans2Json(regionLsit);
} catch (IllegalAccessException | IOException e) {
throw new BusinessException("Region实体转GeoJSON出错");
}
return result;
} }
@Override @Override