空间类相关方法

This commit is contained in:
hukekuan@163.com 2017-08-08 15:49:25 +08:00
parent da30005ad4
commit 53e2838961
34 changed files with 45 additions and 594 deletions

101
pom.xml
View File

@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gis3c</groupId>
<artifactId>gis3c</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>com.c3gis</groupId>
<artifactId>c3gis</artifactId>
<version>1.0.1</version>
<build>
<plugins>
<plugin>
@ -16,6 +16,23 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>src/test/**</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--<classpathPrefix>lib/</classpathPrefix>-->
<!--<mainClass>com.sysware.HelloWorld</mainClass>-->
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
@ -37,98 +54,20 @@
<mybatis.version>3.3.1</mybatis.version>
<slf4j.version>1.7.18</slf4j.version>
<log4j.version>1.2.16</log4j.version>
<!--<shiro.version>1.2.4</shiro.version>-->
<geotools.version>17.0</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>

View File

@ -1,4 +1,4 @@
package com.gis3c.common;/**
package com.c3gis.spatial.common;/**
* Created by hukekuan on 17-7-10.
*/

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
import com.vividsolutions.jts.geom.Geometry;
import org.geotools.data.DataUtilities;

View File

@ -1,4 +1,4 @@
package com.gis3c.entity;
package com.c3gis.spatial.entity;
import org.geotools.data.FeatureSource;
import org.geotools.map.FeatureLayer;

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;/**
package com.c3gis.spatial.entity;/**
* Created by hukekuan on 17-7-10.
*/

View File

@ -1,4 +1,4 @@
package com.gis3c.entity;
package com.c3gis.spatial.entity;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;/**
package com.c3gis.spatial.entity;/**
* Created by hukekuan on 17-7-10.
*/

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;/**
package com.c3gis.spatial.entity;/**
* Created by hukekuan on 17-7-10.
*/

View File

@ -1,8 +1,6 @@
package com.gis3c.entity;
package com.c3gis.spatial.entity;
import com.gis3c.entity.spatial.BaseFeature;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
public class GeoCity extends BaseFeature{
private String code;

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
import java.lang.reflect.Field;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
/**
* Created by hukekuan on 17-7-10.

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
/**
* Created by hukekuan on 17-7-10.

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
/**
* Created by hukekuan on 17-7-10.

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
/**
* Created by hukekuan on 17-7-10.

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
/**
* Created by hukekuan on 17-7-10.

View File

@ -1,4 +1,4 @@
package com.gis3c.entity.spatial;
package com.c3gis.spatial.entity;
/**

View File

@ -1,4 +1,4 @@
package com.gis3c.postgis;
package com.c3gis.spatial.postgis;
import java.io.IOException;
import java.sql.CallableStatement;

View File

@ -1,32 +0,0 @@
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;
public class App {
public static ApplicationContext ContextInit(){
return new ClassPathXmlApplicationContext("classpath:spring-config.xml");
}
public static void main(String[] args) throws IOException, IllegalAccessException {
ApplicationContext context =ContextInit();
PostGISService postGISService = (PostGISService) context.getBean("postGISService");
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));
}
}

View File

@ -1,126 +0,0 @@
package com.gis3c;
import com.gis3c.entity.C3Map;
import com.gis3c.entity.GeoCity;
import com.gis3c.service.PostGISService;
import no.ecc.vectortile.VectorTileDecoder;
import no.ecc.vectortile.VectorTileEncoder;
import org.apache.commons.collections.map.HashedMap;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.styling.SLD;
import org.geotools.styling.SLDParser;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static ApplicationContext ContextInit(){
return new ClassPathXmlApplicationContext("classpath:spring-config.xml");
}
public static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
// public static Style createStyle(File file, FeatureSource featureSource) {
// File sld = toSLDFile(file);
// if (sld != null) {
// return createFromSLD(sld);
// }
//
// SimpleFeatureType schema = (SimpleFeatureType)featureSource.getSchema();
// return JSimpleStyleDialog.showDialog(null, schema);
// }
// public static File toSLDFile(File file) {
// String path = file.getAbsolutePath();
// String base = path.substring(0,path.length()-4);
// String newPath = base + ".sld";
// File sld = new File( newPath );
// if( sld.exists() ){
// return sld;
// }
// newPath = base + ".SLD";
// sld = new File( newPath );
// if( sld.exists() ){
// return sld;
// }
// return null;
// }
// public static Style createFromSLD(File sld) {
// try {
// SLDParser stylereader = new SLDParser(styleFactory, sld.toURI().toURL());
// Style[] style = stylereader.readXML();
// return style[0];
//
// } catch (Exception e) {
// JExceptionReporter.showDialog(e, "Problem creating style");
// }
// return null;
// }
public static void StyleTest()throws IOException{
File file = JFileDataStoreChooser.showOpenFile("shp",null);
if(file == null){
return;
}
File sldFile = new File("D:\\f\\shp\\shijie.sld");
SLDParser styleReader = new SLDParser(styleFactory,sldFile.toURI().toURL());
Style[] styles = styleReader.readXML();
Style style1 = styles[0];
FileDataStore store = FileDataStoreFinder.getDataStore(file);
((ShapefileDataStore)store).setCharset(Charset.forName("GBK"));
SimpleFeatureSource featureSource = store.getFeatureSource();
C3Map map = new C3Map();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource,style1);
map.addLayer(layer);
map.saveImage("D://mapimage//1.jpg",256);
System.out.println("image has saved");
}
public static void copy(Reader in,Writer out) throws IOException {
int c = -1;
while((c = in.read()) != -1) {
out.write(c);
}
}
public static void VectorTilesTest()throws IOException{
ApplicationContext context =ContextInit();
PostGISService postGISService = (PostGISService) context.getBean("postGISService");
List<GeoCity> geoCities = postGISService.AllCities();
VectorTileEncoder encoder = new VectorTileEncoder();
VectorTileDecoder d = new VectorTileDecoder();
byte[] encoded;
geoCities.forEach(city -> {
Map<String,String> cityAttr = new HashMap<>();
cityAttr.put("name",city.getName());
cityAttr.put("code",city.getCode());
// encoder.addFeature("city",cityAttr,city.getGeom());
});
encoded = encoder.encode();
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();
}
}

View File

@ -1,8 +0,0 @@
package com.gis3c.dao;
/**
* Created by hukekuan on 2016/12/28.
*/
public interface HelloDao {
public String SayHello();
}

View File

@ -1,13 +0,0 @@
package com.gis3c.dao;
import com.gis3c.entity.GeoCity;
import java.util.List;
import java.util.Map;
public interface PostGISDao {
public void CommonCreateTable(Map<String,Object> tableStructure);
public void CreateSpatialIndex(String tableName,String geometryColumn);
public List<GeoCity> AllCities();
}

View File

@ -1,28 +0,0 @@
package com.gis3c.dao.impl;
import com.gis3c.dao.HelloDao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* Created by hukekuan on 2016/12/28.
*/
@Repository
public class HelloDaoImpl extends SqlSessionDaoSupport implements HelloDao {
@Resource(name="sqlSessionFactory_GIS") private SqlSessionFactory sqlSessionFactory;
@Override
public String SayHello(){
return "hello wold";
}
@PostConstruct
public void injectSessionFactory(){
super.setSqlSessionFactory(sqlSessionFactory);
}
}

View File

@ -1,41 +0,0 @@
package com.gis3c.dao.impl;
import com.gis3c.dao.PostGISDao;
import com.gis3c.entity.GeoCity;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class PostGISDaoImpl extends SqlSessionDaoSupport implements PostGISDao {
@Resource(name="sqlSessionFactory_GIS")
private SqlSessionFactory sqlSessionFactory;
@PostConstruct
public void injectSessionFactory(){
super.setSqlSessionFactory(sqlSessionFactory);
}
@Override
public void CommonCreateTable(Map<String,Object> tableStructure){
System.out.println(tableStructure);
getSqlSession().update("PostGISSql.commonCreateTable",tableStructure);
}
@Override
public void CreateSpatialIndex(String tableName,String geometryColumn){
Map<String,Object> param = new HashMap<>();
param.put("tableName",tableName);
param.put("geometryColumn",geometryColumn);
param.put("spatialIndexName","sidx_" + tableName +"_" + geometryColumn);
getSqlSession().update("PostGISSql.createSpatialIndex",param);
}
@Override
public List<GeoCity> AllCities(){
return getSqlSession().selectList("PostGISSql.allCity");
}
}

View File

@ -1,8 +0,0 @@
package com.gis3c.service;
/**
* Created by hukekuan on 2016/12/28.
*/
public interface HelloService {
public String SayHello();
}

View File

@ -1,12 +0,0 @@
package com.gis3c.service;
import com.gis3c.entity.GeoCity;
import java.util.List;
import java.util.Map;
public interface PostGISService {
public void CommonCreateTable(String tableName,String geometryColumn,List<Map<String,Object>> columns);
public List<GeoCity> AllCities();
}

View File

@ -1,20 +0,0 @@
package com.gis3c.service.impl;
import com.gis3c.dao.HelloDao;
import com.gis3c.service.HelloService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* Created by hukekuan on 2016/12/28.
*/
@Service("helloService")
public class HelloServiceImpl implements HelloService {
@Resource private HelloDao helloDao;
@Override
public String SayHello(){
return helloDao.SayHello();
}
}

View File

@ -1,41 +0,0 @@
package com.gis3c.service.impl;
import com.gis3c.dao.PostGISDao;
import com.gis3c.entity.GeoCity;
import com.gis3c.service.PostGISService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("postGISService")
public class PostGISServiceImpl implements PostGISService {
@Resource private PostGISDao postGISDao;
public void CommonCreateTable(
String tableName,String geometryColumn,List<Map<String,Object>> columns){
if(tableName == null || "".equals(tableName)
|| geometryColumn == null || "".equals(geometryColumn)
|| columns == null || columns.size() == 0){
throw new IllegalArgumentException("参数错误");
}
Map<String,Object> tableStructure = new HashMap<>();
tableStructure.put("tableName",tableName);
tableStructure.put("columnList",columns);
Map<String,Object> SpatialIndexParam = new HashMap<>();
SpatialIndexParam.put("spatialIndexName","sidx_" + tableName + "_" + geometryColumn);
SpatialIndexParam.put("tableName",tableName);
SpatialIndexParam.put("geometryColumn",geometryColumn);
postGISDao.CommonCreateTable(tableStructure);
postGISDao.CreateSpatialIndex(tableName,geometryColumn);
}
@Override
public List<GeoCity> AllCities(){
return postGISDao.AllCities();
}
}

View File

@ -1,126 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="PostGISSql">
<resultMap id="GeoCityResultMap" type="java.util.HashMap">
<result property="geom" column="geom" javaType="Geometry"/>
</resultMap>
<resultMap id="geoCity" type="com.gis3c.entity.GeoCity">
<result column="code" property="code" />
<result column="name" property="name" />
<result column="geom" property="geometry" />
</resultMap>
<!--创建空间表通用方法-->
<update id="commonCreateTable">
CREATE TABLE ${tableName}
<foreach item="item" index="index" collection="columnList" open="(" separator=",">
<choose>
<when test='item.typeCode == 1'>
${item.columnName} numeric
</when>
<when test='item.typeCode == 2'>
${item.columnName} NUMBER
</when>
<when test='item.typeCode == 3'>
${item.columnName} character varying(254)
</when>
<when test='item.typeCode == 91'>
${item.columnName} geometry(POINT,4326)
</when>
<when test='item.typeCode == 911'>
${item.columnName} geometry(MULTIPOINT,4326)
</when>
<when test='item.typeCode == 92'>
${item.columnName} geometry(LINESTRING,4326)
</when>
<when test='item.typeCode == 922'>
${item.columnName} geometry(MULTILINESTRING,4326)
</when>
<when test='item.typeCode == 93'>
${item.columnName} geometry(POLYGON,4326)
</when>
<when test='item.typeCode == 933'>
${item.columnName} geometry(MULTIPOLYGON,4326)
</when>
</choose>
</foreach>
,CONSTRAINT fid_pkey PRIMARY KEY (fid));
</update>
<!--创建空间索引-->
<update id="createSpatialIndex">
CREATE INDEX ${spatialIndexName}
ON ${tableName}
USING gist
(${geometryColumn});
</update>
<!--查询表是否存在,返回表个数-->
<select id="tableCount" parameterType="java.lang.String" resultType="java.lang.Integer">
select count(1) from pg_class where relname in (lower(#{tableName,jdbcType=VARCHAR}),upper(#{tableName,jdbcType=VARCHAR}));
</select>
<!-- 删除空间表通用方法 -->
<delete id="commonTableRemove" parameterType="java.lang.String">
drop table ${_parameter};
</delete>
<!-- 插入空间信息通用方法 -->
<insert id="commonInsertBatch">
insert into ${tableName}
<foreach item="item" index="index" collection="keyList" open="(" separator="," close=")values">
${item}
</foreach>
<foreach collection="featuresList" item="featureValues" index="index" separator="," >
<foreach item="featureValue" index="featureIndex" collection="featureValues" open=" (" separator="," close=")">
<choose>
<when test='featureValue.key == "fid"'>
#{featureValue.value,jdbcType=INTEGER}
</when>
<when test='featureValue.key == "geo"'>
st_geomfromText(#{featureValue.value,jdbcType=VARCHAR},4326)
</when>
<otherwise>
#{featureValue.value,jdbcType=VARCHAR}
</otherwise>
</choose>
</foreach>
</foreach>
</insert>
<insert id="commonInsert">
insert into ${tableName}
<foreach item="item" index="index" collection="keyList" open="(" separator="," close=")">
${item}
</foreach>
<foreach item="featureValue" index="index" collection="valueList" open=" values(" separator="," close=")">
<choose>
<when test='featureValue.key == "fid"'>
#{featureValue.value,jdbcType=INTEGER}
</when>
<when test='featureValue.key == "geo"'>
st_geomfromText(#{featureValue.value,jdbcType=VARCHAR},4326)
</when>
<otherwise>
#{featureValue.value,jdbcType=VARCHAR}
</otherwise>
</choose>
</foreach>
</insert>
<!--查询表中字段信息-->
<select id="tableColumns" parameterType="java.lang.String" resultType="java.util.HashMap">
select column_name,data_type
from information_schema.columns where table_name = #{tableName,jdbcType=INTEGER} and table_schema='public';
</select>
<!-- 清空表 -->
<delete id="clearSpatialTable" parameterType="java.lang.String">
delete from ${_parameter}
</delete>
<select id="allCity" resultMap="geoCity">
select t.code as code,t.name as name, t.geom
from geocity t;
--where t."CODE"='370300';
</select>
</mapper>

View File

@ -0,0 +1,7 @@
package com.c3gis.spatial;
public class App {
public static void main(String[] args) {
System.out.println("OK");
}
}

View File

@ -1,38 +0,0 @@
package com.gis3c;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -9,6 +9,6 @@
<typeAlias type="com.vividsolutions.jts.geom.Geometry" alias="Geometry" />
</typeAliases>
<typeHandlers>
<typeHandler handler="com.gis3c.postgis.PostGISHandler" javaType="Geometry" />
<typeHandler handler="com.c3gis.spatial.postgis.PostGISHandler" javaType="Geometry" />
</typeHandlers>
</configuration>