空间类相关方法
This commit is contained in:
parent
83e2d1ad22
commit
da30005ad4
@ -3,9 +3,12 @@ package com.gis3c;
|
||||
import com.gis3c.entity.GeoCity;
|
||||
import com.gis3c.service.HelloService;
|
||||
import com.gis3c.service.PostGISService;
|
||||
import org.opengis.feature.simple.SimpleFeature;
|
||||
import org.opengis.feature.simple.SimpleFeatureType;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
@ -15,43 +18,15 @@ public class App {
|
||||
return new ClassPathXmlApplicationContext("classpath:spring-config.xml");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
public static void main(String[] args) throws IOException, IllegalAccessException {
|
||||
ApplicationContext context =ContextInit();
|
||||
HelloService obj = (HelloService) context.getBean("helloService");
|
||||
PostGISService postGISService = (PostGISService) context.getBean("postGISService");
|
||||
|
||||
// List<Map<String,Object>> columns = new ArrayList<>();
|
||||
// Map<String,Object> column = new HashMap<>();
|
||||
// column.put("columnName","fid");
|
||||
// column.put("typeCode",1);
|
||||
// columns.add(column);
|
||||
//
|
||||
// column = new HashMap<>();
|
||||
// column.put("columnName","name");
|
||||
// column.put("typeCode",3);
|
||||
// columns.add(column);
|
||||
//
|
||||
// column = new HashMap<>();
|
||||
// column.put("columnName","geom");
|
||||
// column.put("typeCode",91);
|
||||
// columns.add(column);
|
||||
//
|
||||
// postGISService.CommonCreateTable("test","geom",columns);
|
||||
//
|
||||
// System.out.println(obj.SayHello());
|
||||
|
||||
Optional<GeoCity> result = postGISService.AllCities().stream()
|
||||
.filter(GeoCity::isDefaut)
|
||||
.skip(3)
|
||||
// .map(GeoCity::getName)
|
||||
// .anyMatch(GeoCity::isZB)
|
||||
// .findAny();
|
||||
.findFirst();
|
||||
// .collect(Collectors.toList());
|
||||
|
||||
// result.forEach(item->{
|
||||
// System.out.println(item.getName());
|
||||
// });
|
||||
result.ifPresent(item ->System.out.println(item.getName()));
|
||||
List<GeoCity> geoCities = postGISService.AllCities();
|
||||
List<SimpleFeature> simpleFeatureList = new ArrayList<>();
|
||||
SimpleFeatureType simpleFeatureType = geoCities.get(0).createFeatureType();
|
||||
for(int i = 0,len = geoCities.size();i < len;i++){
|
||||
simpleFeatureList.add(geoCities.get(i).attribute2Feature(simpleFeatureType,Integer.toString(i)));
|
||||
}
|
||||
System.out.println(GeoCity.Attributes2GeoJSON(simpleFeatureList));
|
||||
}
|
||||
}
|
||||
@ -108,20 +108,18 @@ public class Main {
|
||||
Map<String,String> cityAttr = new HashMap<>();
|
||||
cityAttr.put("name",city.getName());
|
||||
cityAttr.put("code",city.getCode());
|
||||
encoder.addFeature("city",cityAttr,city.getGeom());
|
||||
// encoder.addFeature("city",cityAttr,city.getGeom());
|
||||
});
|
||||
encoded = encoder.encode();
|
||||
|
||||
Reader in = new FileReader("C:\\Users\\Administrator\\Desktop\\0.vector.pbf");
|
||||
StringWriter out = new StringWriter();
|
||||
copy(in,out);
|
||||
|
||||
// System.out.println(out.toString());
|
||||
System.out.println(d.decode(out.toString().getBytes()));
|
||||
System.out.println(new String(encoded, "utf8"));
|
||||
try(FileOutputStream fos = new FileOutputStream("/home/hukekuan/0.vector.pbf",true)){
|
||||
fos.write(encoded);
|
||||
fos.flush();
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) throws IOException {
|
||||
VectorTilesTest();
|
||||
//VectorTilesTest();
|
||||
}
|
||||
|
||||
|
||||
|
||||
31
src/main/java/com/gis3c/common/CRSUtil.java
Normal file
31
src/main/java/com/gis3c/common/CRSUtil.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.gis3c.common;/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
|
||||
import org.geotools.factory.CommonFactoryFinder;
|
||||
import org.geotools.referencing.CRS;
|
||||
import org.geotools.referencing.crs.DefaultGeographicCRS;
|
||||
import org.opengis.filter.FilterFactory2;
|
||||
import org.opengis.referencing.FactoryException;
|
||||
import org.opengis.referencing.NoSuchAuthorityCodeException;
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 空间坐标系相关方法
|
||||
* @date 2017-07-10 下午6:31
|
||||
*/
|
||||
public class CRSUtil {
|
||||
private final static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
|
||||
|
||||
public static CoordinateReferenceSystem GetCRSFromSRID(String sridNumber)
|
||||
throws NoSuchAuthorityCodeException, FactoryException {
|
||||
CoordinateReferenceSystem crs = null;
|
||||
if(sridNumber == null || "".equals(sridNumber)){
|
||||
return DefaultGeographicCRS.WGS84;
|
||||
}
|
||||
crs = CRS.decode("EPSG:" + sridNumber);
|
||||
|
||||
return crs;
|
||||
}
|
||||
}
|
||||
@ -1,27 +1,13 @@
|
||||
package com.gis3c.entity;
|
||||
|
||||
import com.vividsolutions.jts.geom.Polygon;
|
||||
import com.gis3c.entity.spatial.BaseFeature;
|
||||
import com.vividsolutions.jts.geom.Geometry;
|
||||
import com.vividsolutions.jts.geom.MultiPolygon;
|
||||
|
||||
public class GeoCity {
|
||||
public class GeoCity extends BaseFeature{
|
||||
private String code;
|
||||
private String name;
|
||||
private Polygon geom;
|
||||
|
||||
public boolean isDefaut(){
|
||||
boolean defaulted = false;
|
||||
if(this.code.contains("370")){
|
||||
defaulted = true;
|
||||
}
|
||||
return defaulted;
|
||||
}
|
||||
|
||||
public boolean isZB(){
|
||||
boolean zb = false;
|
||||
if("370300".equals(this.getCode())){
|
||||
zb = true;
|
||||
}
|
||||
return zb;
|
||||
}
|
||||
private Geometry geometry;
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
@ -38,12 +24,4 @@ public class GeoCity {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Polygon getGeom() {
|
||||
return geom;
|
||||
}
|
||||
|
||||
public void setGeom(Polygon geom) {
|
||||
this.geom = geom;
|
||||
}
|
||||
}
|
||||
|
||||
203
src/main/java/com/gis3c/entity/spatial/BaseFeature.java
Normal file
203
src/main/java/com/gis3c/entity/spatial/BaseFeature.java
Normal file
@ -0,0 +1,203 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
import com.vividsolutions.jts.geom.Geometry;
|
||||
import org.geotools.data.DataUtilities;
|
||||
import org.geotools.data.simple.SimpleFeatureCollection;
|
||||
import org.geotools.feature.simple.SimpleFeatureBuilder;
|
||||
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
|
||||
import org.geotools.geojson.feature.FeatureJSON;
|
||||
import org.geotools.referencing.crs.DefaultGeographicCRS;
|
||||
import org.opengis.feature.simple.SimpleFeature;
|
||||
import org.opengis.feature.simple.SimpleFeatureType;
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 具有空间字段的基类
|
||||
* @date 2017-07-10 下午3:43
|
||||
*/
|
||||
public class BaseFeature {
|
||||
private Geometry geometry;
|
||||
|
||||
public Geometry getGeometry() {
|
||||
return geometry;
|
||||
}
|
||||
|
||||
public void setGeometry(Geometry geometry) {
|
||||
this.geometry = geometry;
|
||||
}
|
||||
|
||||
|
||||
private List<Field> AllFieldes(){
|
||||
List<Field> fieldList = new ArrayList<>();
|
||||
|
||||
Class superClass = BaseFeature.class;
|
||||
Class extendClass = this.getClass();
|
||||
|
||||
while(!superClass.equals(extendClass) && superClass.isAssignableFrom(extendClass)){
|
||||
fieldList.addAll(Arrays.asList(extendClass.getDeclaredFields()));
|
||||
extendClass = extendClass.getSuperclass();
|
||||
}
|
||||
fieldList.addAll(Arrays.asList(superClass.getDeclaredFields()));
|
||||
|
||||
return fieldList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 根据类信息生成对应的SimpleFeatureType对象
|
||||
* @return SimpleFeatureType
|
||||
* @throws NullPointerException
|
||||
* @date 2017-07-10 下午6:39
|
||||
*/
|
||||
public SimpleFeatureType createFeatureType() throws NullPointerException{
|
||||
SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder();
|
||||
SimpleFeatureType featureType=null;
|
||||
if(!geometryChecked()){
|
||||
throw new NullPointerException("空间字段为空");
|
||||
}
|
||||
//CoordinateReferenceSystem crs = this.geometry..GetCRS();
|
||||
CoordinateReferenceSystem crs = null;
|
||||
build.setCRS(crs !=null? crs:DefaultGeographicCRS.WGS84);
|
||||
build.setName(this.getClass().getSimpleName());
|
||||
List<Field> fields = this.AllFieldes();
|
||||
fields.forEach(field -> {
|
||||
build.add(field.getName(),field.getType());
|
||||
});
|
||||
featureType = build.buildFeatureType();
|
||||
|
||||
return featureType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 生成SimpleFeature对象
|
||||
* @param featureType,转换类对应的featureType
|
||||
* @param featureIndex,feature序列号
|
||||
* @return 对象转SimpleFeature
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @date 2017-07-10 下午7:10
|
||||
*/
|
||||
public SimpleFeature attribute2Feature(SimpleFeatureType featureType,String featureIndex)
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
SimpleFeature simpleFeature = null;
|
||||
if(featureType == null){
|
||||
throw new IllegalArgumentException("featureType参数为空");
|
||||
}
|
||||
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
|
||||
List<Field> fields = this.AllFieldes();
|
||||
List<Object> objList = new ArrayList<>();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
objList.add(field.get(this));
|
||||
}
|
||||
simpleFeature = featureBuilder.buildFeature(featureIndex, objList.toArray());
|
||||
return simpleFeature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 对象列表转 SimpleFeatureCollection
|
||||
* @param simpleFeatureList
|
||||
* @return SimpleFeatureCollection
|
||||
* @date 2017-07-11 上午9:46
|
||||
*/
|
||||
public static SimpleFeatureCollection Attributes2Features(List<SimpleFeature> simpleFeatureList){
|
||||
SimpleFeatureCollection simpleFeatureCollection = null;
|
||||
if(simpleFeatureList != null && simpleFeatureList.size() > 0){
|
||||
simpleFeatureCollection = DataUtilities.collection(simpleFeatureList);
|
||||
}
|
||||
return simpleFeatureCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 对象列表转 GeoJSON
|
||||
* @param simpleFeatureList
|
||||
* @return GeoJSON字符串
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String Attributes2GeoJSON(List<SimpleFeature> simpleFeatureList)
|
||||
throws IOException {
|
||||
String result = null;
|
||||
SimpleFeatureCollection simpleFeatureCollection = null;
|
||||
|
||||
if(simpleFeatureList != null && simpleFeatureList.size() > 0){
|
||||
FeatureJSON fjson = new FeatureJSON();
|
||||
StringWriter writer = new StringWriter();
|
||||
simpleFeatureCollection = Attributes2Features(simpleFeatureList);
|
||||
fjson.writeFeatureCollection(simpleFeatureCollection, writer);
|
||||
result = writer.toString();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description SimpleFeatureCollection转 GeoJSON
|
||||
* @param featureCollection
|
||||
* @return GeoJSON字符串
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String FeatureCollection2GeoJson(SimpleFeatureCollection featureCollection)
|
||||
throws IOException{
|
||||
String result = null;
|
||||
FeatureJSON fjson;
|
||||
StringWriter writer;
|
||||
if(featureCollection != null && featureCollection.size() > 0){
|
||||
fjson = new FeatureJSON();
|
||||
writer = new StringWriter();
|
||||
fjson.writeFeatureCollection(featureCollection, writer);
|
||||
result = writer.toString();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description GeoJSON转SimpleFeatureCollection
|
||||
* @param geojsonStr geojson字符串
|
||||
* @return SimpleFeatureCollection
|
||||
* @throws IOException
|
||||
*/
|
||||
public static SimpleFeatureCollection GeoJson2FeatureCollection(String geojsonStr)
|
||||
throws IOException{
|
||||
SimpleFeatureCollection result = null;
|
||||
FeatureJSON fjson = new FeatureJSON();
|
||||
|
||||
result = (SimpleFeatureCollection) fjson.readFeatureCollection(new ByteArrayInputStream(geojsonStr.getBytes()));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 判断对象是否存在空间字段
|
||||
* @return true,存在空间字段;false,不存在
|
||||
* @date 2017-07-10 下午4:42
|
||||
*/
|
||||
private boolean geometryChecked(){
|
||||
boolean isExist = false;
|
||||
if(this.geometry != null){
|
||||
isExist = true;
|
||||
}
|
||||
|
||||
return isExist;
|
||||
}
|
||||
}
|
||||
24
src/main/java/com/gis3c/entity/spatial/C3LineString.java
Normal file
24
src/main/java/com/gis3c/entity/spatial/C3LineString.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.gis3c.entity.spatial;/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
|
||||
import com.vividsolutions.jts.geom.CoordinateSequence;
|
||||
import com.vividsolutions.jts.geom.GeometryFactory;
|
||||
import com.vividsolutions.jts.geom.LineString;
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 折线数据
|
||||
* @date 2017-07-10 下午4:21
|
||||
*/
|
||||
public class C3LineString extends LineString implements ILineString {
|
||||
public C3LineString(CoordinateSequence points, GeometryFactory factory) {
|
||||
super(points, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CoordinateReferenceSystem GetCRS() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
24
src/main/java/com/gis3c/entity/spatial/C3Point.java
Normal file
24
src/main/java/com/gis3c/entity/spatial/C3Point.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.gis3c.entity.spatial;/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
|
||||
import com.vividsolutions.jts.geom.CoordinateSequence;
|
||||
import com.vividsolutions.jts.geom.GeometryFactory;
|
||||
import com.vividsolutions.jts.geom.Point;
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 自定义点数据
|
||||
* @date 2017-07-10 下午4:19
|
||||
*/
|
||||
public class C3Point extends Point implements IPoint {
|
||||
public C3Point(CoordinateSequence coordinates, GeometryFactory factory) {
|
||||
super(coordinates, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CoordinateReferenceSystem GetCRS() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
24
src/main/java/com/gis3c/entity/spatial/C3Polygon.java
Normal file
24
src/main/java/com/gis3c/entity/spatial/C3Polygon.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.gis3c.entity.spatial;/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
|
||||
import com.vividsolutions.jts.geom.GeometryFactory;
|
||||
import com.vividsolutions.jts.geom.LinearRing;
|
||||
import com.vividsolutions.jts.geom.Polygon;
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 单面数据
|
||||
* @date 2017-07-10 下午4:23
|
||||
*/
|
||||
public class C3Polygon extends Polygon implements IPolygon {
|
||||
public C3Polygon(LinearRing shell, LinearRing[] holes, GeometryFactory factory) {
|
||||
super(shell, holes, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CoordinateReferenceSystem GetCRS() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
17
src/main/java/com/gis3c/entity/spatial/IFeature.java
Normal file
17
src/main/java/com/gis3c/entity/spatial/IFeature.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IFeature {
|
||||
/**
|
||||
* @author hukekuan
|
||||
* @Description 获取所有的Field,前提是要有Geometry属性
|
||||
* @date 2017-07-10 下午4:36
|
||||
* @return 返回所有的Field
|
||||
*/
|
||||
public List<Field> AllFieldes();
|
||||
}
|
||||
10
src/main/java/com/gis3c/entity/spatial/IGeometry.java
Normal file
10
src/main/java/com/gis3c/entity/spatial/IGeometry.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IGeometry {
|
||||
public CoordinateReferenceSystem GetCRS();
|
||||
}
|
||||
7
src/main/java/com/gis3c/entity/spatial/ILineString.java
Normal file
7
src/main/java/com/gis3c/entity/spatial/ILineString.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface ILineString extends IGeometry {
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IMultiLineString extends IGeometry {
|
||||
}
|
||||
7
src/main/java/com/gis3c/entity/spatial/IMultiPoint.java
Normal file
7
src/main/java/com/gis3c/entity/spatial/IMultiPoint.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IMultiPoint extends IGeometry {
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IMultiPolygon extends IGeometry {
|
||||
}
|
||||
7
src/main/java/com/gis3c/entity/spatial/IPoint.java
Normal file
7
src/main/java/com/gis3c/entity/spatial/IPoint.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IPoint extends IGeometry {
|
||||
}
|
||||
8
src/main/java/com/gis3c/entity/spatial/IPolygon.java
Normal file
8
src/main/java/com/gis3c/entity/spatial/IPolygon.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.gis3c.entity.spatial;
|
||||
|
||||
|
||||
/**
|
||||
* Created by hukekuan on 17-7-10.
|
||||
*/
|
||||
public interface IPolygon extends IGeometry {
|
||||
}
|
||||
@ -7,7 +7,7 @@
|
||||
<resultMap id="geoCity" type="com.gis3c.entity.GeoCity">
|
||||
<result column="code" property="code" />
|
||||
<result column="name" property="name" />
|
||||
<result column="geom" property="geom" javaType="Geometry" />
|
||||
<result column="geom" property="geometry" />
|
||||
</resultMap>
|
||||
<!--创建空间表通用方法-->
|
||||
<update id="commonCreateTable">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user