From da30005ad4ed542d6635de221db8f9315ad3fbfb Mon Sep 17 00:00:00 2001 From: "hukekuan@163.com" Date: Tue, 11 Jul 2017 16:49:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A9=BA=E9=97=B4=E7=B1=BB=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/gis3c/App.java | 47 +--- src/main/java/com/gis3c/Main.java | 14 +- src/main/java/com/gis3c/common/CRSUtil.java | 31 +++ src/main/java/com/gis3c/entity/GeoCity.java | 32 +-- .../com/gis3c/entity/spatial/BaseFeature.java | 203 ++++++++++++++++++ .../gis3c/entity/spatial/C3LineString.java | 24 +++ .../com/gis3c/entity/spatial/C3Point.java | 24 +++ .../com/gis3c/entity/spatial/C3Polygon.java | 24 +++ .../com/gis3c/entity/spatial/IFeature.java | 17 ++ .../com/gis3c/entity/spatial/IGeometry.java | 10 + .../com/gis3c/entity/spatial/ILineString.java | 7 + .../entity/spatial/IMultiLineString.java | 7 + .../com/gis3c/entity/spatial/IMultiPoint.java | 7 + .../gis3c/entity/spatial/IMultiPolygon.java | 7 + .../java/com/gis3c/entity/spatial/IPoint.java | 7 + .../com/gis3c/entity/spatial/IPolygon.java | 8 + .../java/com/gis3c/sqlmaps/PostGISMapper.xml | 2 +- 17 files changed, 399 insertions(+), 72 deletions(-) create mode 100644 src/main/java/com/gis3c/common/CRSUtil.java create mode 100644 src/main/java/com/gis3c/entity/spatial/BaseFeature.java create mode 100644 src/main/java/com/gis3c/entity/spatial/C3LineString.java create mode 100644 src/main/java/com/gis3c/entity/spatial/C3Point.java create mode 100644 src/main/java/com/gis3c/entity/spatial/C3Polygon.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IFeature.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IGeometry.java create mode 100644 src/main/java/com/gis3c/entity/spatial/ILineString.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IMultiLineString.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IMultiPoint.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IMultiPolygon.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IPoint.java create mode 100644 src/main/java/com/gis3c/entity/spatial/IPolygon.java diff --git a/src/main/java/com/gis3c/App.java b/src/main/java/com/gis3c/App.java index 050c045..74a9584 100644 --- a/src/main/java/com/gis3c/App.java +++ b/src/main/java/com/gis3c/App.java @@ -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> columns = new ArrayList<>(); -// Map 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 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 geoCities = postGISService.AllCities(); + List 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)); } } \ No newline at end of file diff --git a/src/main/java/com/gis3c/Main.java b/src/main/java/com/gis3c/Main.java index c7b58ea..8d7a179 100644 --- a/src/main/java/com/gis3c/Main.java +++ b/src/main/java/com/gis3c/Main.java @@ -108,20 +108,18 @@ public class Main { Map 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(); } diff --git a/src/main/java/com/gis3c/common/CRSUtil.java b/src/main/java/com/gis3c/common/CRSUtil.java new file mode 100644 index 0000000..3859a1a --- /dev/null +++ b/src/main/java/com/gis3c/common/CRSUtil.java @@ -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; + } +} diff --git a/src/main/java/com/gis3c/entity/GeoCity.java b/src/main/java/com/gis3c/entity/GeoCity.java index af2ba3f..7a77d28 100644 --- a/src/main/java/com/gis3c/entity/GeoCity.java +++ b/src/main/java/com/gis3c/entity/GeoCity.java @@ -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; - } } diff --git a/src/main/java/com/gis3c/entity/spatial/BaseFeature.java b/src/main/java/com/gis3c/entity/spatial/BaseFeature.java new file mode 100644 index 0000000..7c810cc --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/BaseFeature.java @@ -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 AllFieldes(){ + List 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 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 fields = this.AllFieldes(); + List 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 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 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; + } +} diff --git a/src/main/java/com/gis3c/entity/spatial/C3LineString.java b/src/main/java/com/gis3c/entity/spatial/C3LineString.java new file mode 100644 index 0000000..fcbe390 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/C3LineString.java @@ -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; + } +} diff --git a/src/main/java/com/gis3c/entity/spatial/C3Point.java b/src/main/java/com/gis3c/entity/spatial/C3Point.java new file mode 100644 index 0000000..df23d45 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/C3Point.java @@ -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; + } +} diff --git a/src/main/java/com/gis3c/entity/spatial/C3Polygon.java b/src/main/java/com/gis3c/entity/spatial/C3Polygon.java new file mode 100644 index 0000000..c964cd4 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/C3Polygon.java @@ -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; + } +} diff --git a/src/main/java/com/gis3c/entity/spatial/IFeature.java b/src/main/java/com/gis3c/entity/spatial/IFeature.java new file mode 100644 index 0000000..dfd54cc --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IFeature.java @@ -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 AllFieldes(); +} diff --git a/src/main/java/com/gis3c/entity/spatial/IGeometry.java b/src/main/java/com/gis3c/entity/spatial/IGeometry.java new file mode 100644 index 0000000..c238b73 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IGeometry.java @@ -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(); +} diff --git a/src/main/java/com/gis3c/entity/spatial/ILineString.java b/src/main/java/com/gis3c/entity/spatial/ILineString.java new file mode 100644 index 0000000..257d3b3 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/ILineString.java @@ -0,0 +1,7 @@ +package com.gis3c.entity.spatial; + +/** + * Created by hukekuan on 17-7-10. + */ +public interface ILineString extends IGeometry { +} diff --git a/src/main/java/com/gis3c/entity/spatial/IMultiLineString.java b/src/main/java/com/gis3c/entity/spatial/IMultiLineString.java new file mode 100644 index 0000000..acb88a1 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IMultiLineString.java @@ -0,0 +1,7 @@ +package com.gis3c.entity.spatial; + +/** + * Created by hukekuan on 17-7-10. + */ +public interface IMultiLineString extends IGeometry { +} diff --git a/src/main/java/com/gis3c/entity/spatial/IMultiPoint.java b/src/main/java/com/gis3c/entity/spatial/IMultiPoint.java new file mode 100644 index 0000000..c3bd6f5 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IMultiPoint.java @@ -0,0 +1,7 @@ +package com.gis3c.entity.spatial; + +/** + * Created by hukekuan on 17-7-10. + */ +public interface IMultiPoint extends IGeometry { +} diff --git a/src/main/java/com/gis3c/entity/spatial/IMultiPolygon.java b/src/main/java/com/gis3c/entity/spatial/IMultiPolygon.java new file mode 100644 index 0000000..da74a75 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IMultiPolygon.java @@ -0,0 +1,7 @@ +package com.gis3c.entity.spatial; + +/** + * Created by hukekuan on 17-7-10. + */ +public interface IMultiPolygon extends IGeometry { +} diff --git a/src/main/java/com/gis3c/entity/spatial/IPoint.java b/src/main/java/com/gis3c/entity/spatial/IPoint.java new file mode 100644 index 0000000..57cbbbe --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IPoint.java @@ -0,0 +1,7 @@ +package com.gis3c.entity.spatial; + +/** + * Created by hukekuan on 17-7-10. + */ +public interface IPoint extends IGeometry { +} diff --git a/src/main/java/com/gis3c/entity/spatial/IPolygon.java b/src/main/java/com/gis3c/entity/spatial/IPolygon.java new file mode 100644 index 0000000..964d5c8 --- /dev/null +++ b/src/main/java/com/gis3c/entity/spatial/IPolygon.java @@ -0,0 +1,8 @@ +package com.gis3c.entity.spatial; + + +/** + * Created by hukekuan on 17-7-10. + */ +public interface IPolygon extends IGeometry { +} diff --git a/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml b/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml index 8ad7fb7..2b377b2 100644 --- a/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml +++ b/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml @@ -7,7 +7,7 @@ - +