├── .gitattributes ├── .gitignore ├── 00-jackson-datatype-jts ├── .gitignore ├── build.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── bedatadriven │ │ │ └── jackson │ │ │ └── datatype │ │ │ └── jts │ │ │ ├── GeoJson.java │ │ │ ├── JtsModule.java │ │ │ ├── JtsModule3D.java │ │ │ ├── parsers │ │ │ ├── BaseParser.java │ │ │ ├── GenericGeometryParser.java │ │ │ ├── GeometryCollectionParser.java │ │ │ ├── GeometryParser.java │ │ │ ├── LineStringParser.java │ │ │ ├── MultiLineStringParser.java │ │ │ ├── MultiPointParser.java │ │ │ ├── MultiPolygonParser.java │ │ │ ├── PointParser.java │ │ │ └── PolygonParser.java │ │ │ └── serialization │ │ │ ├── GeometryDeserializer.java │ │ │ └── GeometrySerializer.java │ └── resources │ │ └── META-INF │ │ ├── LICENSE │ │ └── services │ │ └── com.fasterxml.jackson.databind.Module │ └── test │ └── java │ └── com │ └── bedatadriven │ └── jackson │ └── datatype │ └── jts │ ├── BaseJtsModuleTest.java │ ├── D3DPointTest.java │ ├── GeometryCollectionParserTest.java │ ├── JtsModuleTest.java │ ├── LineStringTest.java │ ├── MultiLineStringTest.java │ ├── MultiPointTest.java │ ├── MultiPolygonTest.java │ ├── PointTest.java │ ├── PolygonTest.java │ └── PolygonWithHolesTest.java ├── 00-postgis-jdbc-jts ├── build.gradle └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── postgis │ │ │ └── jts │ │ │ ├── JTSShape.java │ │ │ ├── JtsBinaryParser.java │ │ │ ├── JtsBinaryWriter.java │ │ │ ├── JtsGeometry.java │ │ │ ├── JtsGisWrapper.java │ │ │ └── JtsWrapper.java │ └── javadoc │ │ └── overview.html │ └── test │ ├── java │ └── org │ │ └── postgis │ │ └── jts │ │ └── JtsParserTest.java │ └── resources │ ├── logback-test.xml │ └── testng.xml ├── 01-postgis ├── README.md ├── build.gradle ├── img │ ├── data-output1.png │ ├── data-output2.png │ ├── pgadmin.png │ ├── postgis安装.png │ ├── query-tool.png │ ├── view-table.png │ └── 新建数据库.png └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── lonelyleaf │ │ │ └── gis │ │ │ ├── GisDemoApplication.java │ │ │ ├── config │ │ │ ├── JacksonConfig.java │ │ │ ├── MybatisPlusConfig.java │ │ │ ├── SwaggerConfig.java │ │ │ └── SwaggerProperties.java │ │ │ ├── db │ │ │ └── DriverWrapper.java │ │ │ ├── dto │ │ │ ├── GpsDto.java │ │ │ └── SimplePoint.java │ │ │ ├── entity │ │ │ └── GpsEntity.java │ │ │ ├── mapper │ │ │ └── GpsMapper.java │ │ │ ├── mybatis │ │ │ └── postgis │ │ │ │ ├── AbstractGeometryTypeHandler.java │ │ │ │ ├── LineStringTypeHandler.java │ │ │ │ ├── MultiPointTypeHandler.java │ │ │ │ ├── PointTypeHandler.java │ │ │ │ └── PolygonTypeHandler.java │ │ │ ├── repo │ │ │ └── GpsRepo.java │ │ │ ├── rest │ │ │ └── GpsController.java │ │ │ ├── service │ │ │ └── GpsService.java │ │ │ └── util │ │ │ └── JsonUtil.java │ └── resources │ │ ├── application.yaml │ │ └── db │ │ └── migration │ │ ├── V1.0__init.sql │ │ ├── V1.1__gps_sample_data.sql │ │ └── V1.2__index_gps.sql │ └── test │ └── java │ └── com │ └── github │ └── lonelyleaf │ └── gis │ └── GisDemoApplicationTests.java ├── 02-jts ├── README.md ├── build.gradle ├── img │ ├── bbox.png │ ├── bind-roads │ │ ├── 01-bind-roads.png │ │ ├── 02-bind-roads.png │ │ └── open-street-map-layers.png │ ├── geometries │ │ ├── lines.png │ │ ├── points.png │ │ └── polygons.png │ ├── jts-data-output.png │ ├── jts-geometry.png │ ├── qgis-import-geojson │ │ ├── 00-add-layer.png │ │ ├── 01-add-layer.png │ │ ├── 02-output.png │ │ └── 03-outputs.png │ ├── qgis-import-sql-query │ │ ├── 01-qgis-import-sql-query.png │ │ ├── 02-qgis-import-sql-query.png │ │ ├── 03-qgis-import-sql-query.png │ │ └── 04-qgis-import-sql-query.png │ ├── relations │ │ ├── st_crosses.png │ │ ├── st_disjoint.png │ │ ├── st_dwithin.png │ │ ├── st_equals.png │ │ ├── st_intersects.png │ │ ├── st_overlaps.png │ │ ├── st_touches.png │ │ └── st_within.png │ ├── rtree-hierarchy.png │ └── sql │ │ ├── district-explain.png │ │ └── sql-district-output.png ├── sql │ └── 绑路.sql ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── github │ │ │ │ └── lonelyleaf │ │ │ │ └── gis │ │ │ │ ├── JtsGisDemoApplication.java │ │ │ │ ├── config │ │ │ │ ├── JacksonConfig.java │ │ │ │ ├── MybatisPlusConfig.java │ │ │ │ ├── SwaggerConfig.java │ │ │ │ └── SwaggerProperties.java │ │ │ │ ├── dto │ │ │ │ ├── GpsDto.java │ │ │ │ ├── GpsLine.java │ │ │ │ └── SimplePoint.java │ │ │ │ ├── entity │ │ │ │ ├── DistrictEntity.java │ │ │ │ ├── DistrictGeomEntity.java │ │ │ │ └── GpsEntity.java │ │ │ │ ├── mapper │ │ │ │ └── GpsMapper.java │ │ │ │ ├── mybatis │ │ │ │ ├── jts │ │ │ │ │ ├── AbstractJtsGeometryTypeHandler.java │ │ │ │ │ ├── JtsLineStringTypeHandler.java │ │ │ │ │ ├── JtsLinearRingTypeHandler.java │ │ │ │ │ ├── JtsMultiLineStringTypeHandler.java │ │ │ │ │ ├── JtsMultiPointTypeHandler.java │ │ │ │ │ ├── JtsMultiPolygonTypeHandler.java │ │ │ │ │ ├── JtsPointTypeHandler.java │ │ │ │ │ └── JtsPolygonTypeHandler.java │ │ │ │ └── wrapper │ │ │ │ │ └── PostgisWrapper.java │ │ │ │ ├── repo │ │ │ │ ├── DistrictGeomRepo.java │ │ │ │ ├── DistrictRepo.java │ │ │ │ ├── GisRepo.java │ │ │ │ └── GpsRepo.java │ │ │ │ ├── rest │ │ │ │ ├── DistrictController.java │ │ │ │ └── GpsController.java │ │ │ │ ├── service │ │ │ │ ├── DistrictService.java │ │ │ │ └── GpsService.java │ │ │ │ └── util │ │ │ │ ├── JsonUtil.java │ │ │ │ └── JtsUtil.java │ │ └── resources │ │ │ ├── application.yaml │ │ │ └── db │ │ │ └── migration │ │ │ ├── V1.0__init.sql │ │ │ ├── V1.1__gps_sample_data.sql │ │ │ ├── V1.2__index_gps.sql │ │ │ ├── V1.3__guizhou_boundary.sql │ │ │ ├── V1.4__guizhou_data.sql │ │ │ ├── V1.5__guizhou_index.sql │ │ │ ├── V1.6__zunyi_roads.sql │ │ │ ├── V1.7__zunyi_roads_data.sql │ │ │ └── V1.8__zunyi_roads_index.sql │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── lonelyleaf │ │ └── gis │ │ ├── PostgisJtsTest.java │ │ ├── PostgisWrapperTest.java │ │ └── TestGeoJson.java ├── 使用JTS与postgis进行空间数据交互.md └── 在java程序中使用postgis进行地理位置数据分析.md ├── 03-jpa ├── build.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── lonelyleaf │ │ │ └── gis │ │ │ ├── JpaGisDemoApplication.java │ │ │ ├── config │ │ │ ├── JacksonConfig.java │ │ │ ├── JpaConfig.java │ │ │ ├── SwaggerConfig.java │ │ │ └── SwaggerProperties.java │ │ │ ├── dto │ │ │ ├── GpsDto.java │ │ │ ├── GpsLine.java │ │ │ └── SimplePoint.java │ │ │ ├── entity │ │ │ ├── DistrictEntity.java │ │ │ ├── DistrictGeomEntity.java │ │ │ └── GpsEntity.java │ │ │ ├── mapper │ │ │ └── GpsMapper.java │ │ │ ├── repo │ │ │ ├── DistrictRepo.java │ │ │ └── GpsRepo.java │ │ │ ├── rest │ │ │ ├── DistrictController.java │ │ │ └── GpsController.java │ │ │ ├── service │ │ │ ├── DistrictService.java │ │ │ └── GpsService.java │ │ │ └── util │ │ │ ├── JsonUtil.java │ │ │ └── JtsUtil.java │ └── resources │ │ ├── application.yaml │ │ └── db │ │ └── migration │ │ ├── V1.0__init.sql │ │ ├── V1.1__gps_sample_data.sql │ │ ├── V1.2__index_gps.sql │ │ ├── V1.3__guizhou_boundary.sql │ │ ├── V1.4__guizhou_data.sql │ │ ├── V1.5__guizhou_index.sql │ │ ├── V1.6__zunyi_roads.sql │ │ ├── V1.7__zunyi_roads_data.sql │ │ └── V1.8__zunyi_roads_index.sql │ └── test │ └── java │ └── com │ └── github │ └── lonelyleaf │ └── gis │ └── TestGeoJson.java ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lombok.config └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sql filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/** 6 | !**/src/test/** 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | 17 | ### IntelliJ IDEA ### 18 | .idea 19 | *.iws 20 | *.iml 21 | *.ipr 22 | out/ 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | 31 | ### VS Code ### 32 | .vscode/ 33 | 34 | **/build/ 35 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | *.class 4 | 5 | # Mobile Tools for Java (J2ME) 6 | .mtj.tmp/ 7 | 8 | # Package Files # 9 | *.jar 10 | *.war 11 | *.ear 12 | 13 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 14 | hs_err_pid* 15 | ### Gradle template 16 | .gradle 17 | build/ 18 | 19 | # Ignore Gradle GUI config 20 | gradle-app.setting 21 | 22 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 23 | !gradle-wrapper.jar 24 | ### JetBrains template 25 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 26 | 27 | *.iml 28 | 29 | ## Directory-based project format: 30 | .idea/ 31 | # if you remove the above rule, at least ignore the following: 32 | 33 | # User-specific stuff: 34 | # .idea/workspace.xml 35 | # .idea/tasks.xml 36 | # .idea/dictionaries 37 | 38 | # Sensitive or high-churn files: 39 | # .idea/dataSources.ids 40 | # .idea/dataSources.xml 41 | # .idea/sqlDataSources.xml 42 | # .idea/dynamic.xml 43 | # .idea/uiDesigner.xml 44 | 45 | # Gradle: 46 | # .idea/gradle.xml 47 | # .idea/libraries 48 | 49 | # Mongo Explorer plugin: 50 | # .idea/mongoSettings.xml 51 | 52 | ## File-based project format: 53 | *.ipr 54 | *.iws 55 | 56 | ## Plugin-specific files: 57 | 58 | # IntelliJ 59 | /out/ 60 | 61 | # mpeltonen/sbt-idea plugin 62 | .idea_modules/ 63 | 64 | # JIRA plugin 65 | atlassian-ide-plugin.xml 66 | 67 | # Crashlytics plugin (for Android Studio and IntelliJ) 68 | com_crashlytics_export_strings.xml 69 | crashlytics.properties 70 | crashlytics-build.properties 71 | ### Maven template 72 | target/ 73 | pom.xml.tag 74 | pom.xml.releaseBackup 75 | pom.xml.versionsBackup 76 | pom.xml.next 77 | release.properties 78 | dependency-reduced-pom.xml 79 | buildNumber.properties 80 | .mvn/timing.properties 81 | 82 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | apply plugin: 'idea' 3 | apply plugin: 'maven' 4 | 5 | group 'com.github.lonelyleaf' 6 | version '2.3-SNAPSHOT' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | sourceCompatibility = 1.8 13 | targetCompatibility = 1.8 14 | 15 | dependencies { 16 | implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.1' 17 | implementation "org.locationtech.jts:jts-core:1.16.1" 18 | 19 | testCompile 'junit:junit:4.11' 20 | testCompile('org.easymock:easymock:3.2') 21 | } -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/GeoJson.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | 4 | public class GeoJson { 5 | public static final String POINT = "Point"; 6 | public static final String LINE_STRING = "LineString"; 7 | public static final String POLYGON = "Polygon"; 8 | 9 | public static final String MULTI_POINT = "MultiPoint"; 10 | public static final String MULTI_LINE_STRING = "MultiLineString"; 11 | public static final String MULTI_POLYGON = "MultiPolygon"; 12 | 13 | public static final String GEOMETRY_COLLECTION = "GeometryCollection"; 14 | 15 | public static final String TYPE = "type"; 16 | 17 | public static final String GEOMETRIES = "geometries"; 18 | 19 | public static final String COORDINATES = "coordinates"; 20 | } 21 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/JtsModule.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.parsers.*; 4 | import com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer; 5 | import com.bedatadriven.jackson.datatype.jts.serialization.GeometrySerializer; 6 | import com.fasterxml.jackson.core.Version; 7 | import com.fasterxml.jackson.databind.module.SimpleModule; 8 | import org.locationtech.jts.geom.*; 9 | 10 | public class JtsModule extends SimpleModule { 11 | 12 | public JtsModule() { 13 | this(new GeometryFactory()); 14 | } 15 | 16 | public JtsModule(GeometryFactory geometryFactory) { 17 | super("JtsModule", new Version(1, 0, 0, null,"com.bedatadriven","jackson-datatype-jts")); 18 | 19 | addSerializer(Geometry.class, new GeometrySerializer()); 20 | GenericGeometryParser genericGeometryParser = new GenericGeometryParser(geometryFactory); 21 | addDeserializer(Geometry.class, new GeometryDeserializer(genericGeometryParser)); 22 | addDeserializer(Point.class, new GeometryDeserializer(new PointParser(geometryFactory))); 23 | addDeserializer(MultiPoint.class, new GeometryDeserializer(new MultiPointParser(geometryFactory))); 24 | addDeserializer(LineString.class, new GeometryDeserializer(new LineStringParser(geometryFactory))); 25 | addDeserializer(MultiLineString.class, new GeometryDeserializer(new MultiLineStringParser(geometryFactory))); 26 | addDeserializer(Polygon.class, new GeometryDeserializer(new PolygonParser(geometryFactory))); 27 | addDeserializer(MultiPolygon.class, new GeometryDeserializer(new MultiPolygonParser(geometryFactory))); 28 | addDeserializer(GeometryCollection.class, new GeometryDeserializer(new GeometryCollectionParser(geometryFactory, genericGeometryParser))); 29 | } 30 | 31 | @Override 32 | public void setupModule(SetupContext context) { 33 | super.setupModule(context); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/JtsModule3D.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.parsers.*; 4 | import com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer; 5 | import com.bedatadriven.jackson.datatype.jts.serialization.GeometrySerializer; 6 | import com.fasterxml.jackson.core.Version; 7 | import com.fasterxml.jackson.databind.module.SimpleModule; 8 | import org.locationtech.jts.geom.*; 9 | 10 | public class JtsModule3D extends SimpleModule { 11 | 12 | public JtsModule3D() { 13 | this(new GeometryFactory()); 14 | } 15 | 16 | public JtsModule3D(GeometryFactory geometryFactory) { 17 | super("JtsModule3D", new Version(1, 0, 0, null,"com.bedatadriven","jackson-datatype-jts")); 18 | 19 | addSerializer(Geometry.class, new GeometrySerializer()); 20 | GenericGeometryParser genericGeometryParser = new GenericGeometryParser(geometryFactory); 21 | addDeserializer(Geometry.class, new GeometryDeserializer(genericGeometryParser)); 22 | addDeserializer(Point.class, new GeometryDeserializer(new PointParser(geometryFactory))); 23 | addDeserializer(MultiPoint.class, new GeometryDeserializer(new MultiPointParser(geometryFactory))); 24 | addDeserializer(LineString.class, new GeometryDeserializer(new LineStringParser(geometryFactory))); 25 | addDeserializer(MultiLineString.class, new GeometryDeserializer(new MultiLineStringParser(geometryFactory))); 26 | addDeserializer(Polygon.class, new GeometryDeserializer(new PolygonParser(geometryFactory))); 27 | addDeserializer(MultiPolygon.class, new GeometryDeserializer(new MultiPolygonParser(geometryFactory))); 28 | addDeserializer(GeometryCollection.class, new GeometryDeserializer(new GeometryCollectionParser(geometryFactory, genericGeometryParser))); 29 | } 30 | 31 | @Override 32 | public void setupModule(SetupContext context) { 33 | super.setupModule(context); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/BaseParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import org.locationtech.jts.geom.GeometryFactory; 4 | 5 | /** 6 | * Created by mihaildoronin on 11/11/15. 7 | */ 8 | public class BaseParser { 9 | 10 | protected GeometryFactory geometryFactory; 11 | 12 | public BaseParser(GeometryFactory geometryFactory) { 13 | this.geometryFactory = geometryFactory; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/GenericGeometryParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.Geometry; 6 | import org.locationtech.jts.geom.GeometryFactory; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.*; 12 | 13 | /** 14 | * Created by mihaildoronin on 11/11/15. 15 | */ 16 | public class GenericGeometryParser extends BaseParser implements GeometryParser { 17 | 18 | private Map parsers; 19 | 20 | public GenericGeometryParser(GeometryFactory geometryFactory) { 21 | super(geometryFactory); 22 | parsers = new HashMap(); 23 | parsers.put(POINT, new PointParser(geometryFactory)); 24 | parsers.put(MULTI_POINT, new MultiPointParser(geometryFactory)); 25 | parsers.put(LINE_STRING, new LineStringParser(geometryFactory)); 26 | parsers.put(MULTI_LINE_STRING, new MultiLineStringParser(geometryFactory)); 27 | parsers.put(POLYGON, new PolygonParser(geometryFactory)); 28 | parsers.put(MULTI_POLYGON, new MultiPolygonParser(geometryFactory)); 29 | parsers.put(GEOMETRY_COLLECTION, new GeometryCollectionParser(geometryFactory, this)); 30 | } 31 | 32 | @Override 33 | public Geometry geometryFromJson(JsonNode node) throws JsonMappingException { 34 | String typeName = node.get(TYPE).asText(); 35 | GeometryParser parser = parsers.get(typeName); 36 | if (parser != null) { 37 | return parser.geometryFromJson(node); 38 | } 39 | else { 40 | throw new JsonMappingException("Invalid geometry type: " + typeName); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/GeometryCollectionParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.Geometry; 6 | import org.locationtech.jts.geom.GeometryCollection; 7 | import org.locationtech.jts.geom.GeometryFactory; 8 | 9 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.GEOMETRIES; 10 | 11 | /** 12 | * Created by mihaildoronin on 11/11/15. 13 | */ 14 | public class GeometryCollectionParser extends BaseParser implements GeometryParser { 15 | 16 | private GenericGeometryParser genericGeometriesParser; 17 | 18 | public GeometryCollectionParser(GeometryFactory geometryFactory, GenericGeometryParser genericGeometriesParser) { 19 | super(geometryFactory); 20 | this.genericGeometriesParser = genericGeometriesParser; 21 | } 22 | 23 | private Geometry[] geometriesFromJson(JsonNode arrayOfGeoms) throws JsonMappingException { 24 | Geometry[] items = new Geometry[arrayOfGeoms.size()]; 25 | for(int i=0;i!=arrayOfGeoms.size();++i) { 26 | items[i] = genericGeometriesParser.geometryFromJson(arrayOfGeoms.get(i)); 27 | } 28 | return items; 29 | } 30 | 31 | @Override 32 | public GeometryCollection geometryFromJson(JsonNode node) throws JsonMappingException { 33 | return geometryFactory.createGeometryCollection( 34 | geometriesFromJson(node.get(GEOMETRIES))); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/GeometryParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.Geometry; 6 | 7 | /** 8 | * Created by mihaildoronin on 11/11/15. 9 | */ 10 | public interface GeometryParser { 11 | 12 | T geometryFromJson(JsonNode node) throws JsonMappingException; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/LineStringParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.GeometryFactory; 6 | import org.locationtech.jts.geom.LineString; 7 | 8 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.COORDINATES; 9 | 10 | /** 11 | * Created by mihaildoronin on 11/11/15. 12 | */ 13 | public class LineStringParser extends BaseParser implements GeometryParser { 14 | 15 | public LineStringParser(GeometryFactory geometryFactory) { 16 | super(geometryFactory); 17 | } 18 | 19 | public LineString lineStringFromJson(JsonNode root) { 20 | return geometryFactory.createLineString( 21 | PointParser.coordinatesFromJson(root.get(COORDINATES))); 22 | } 23 | 24 | @Override 25 | public LineString geometryFromJson(JsonNode node) throws JsonMappingException { 26 | return lineStringFromJson(node); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/MultiLineStringParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.GeometryFactory; 6 | import org.locationtech.jts.geom.LineString; 7 | import org.locationtech.jts.geom.MultiLineString; 8 | 9 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.COORDINATES; 10 | 11 | /** 12 | * Created by mihaildoronin on 11/11/15. 13 | */ 14 | public class MultiLineStringParser extends BaseParser implements GeometryParser { 15 | 16 | public MultiLineStringParser(GeometryFactory geometryFactory) { 17 | super(geometryFactory); 18 | } 19 | 20 | public MultiLineString multiLineStringFromJson(JsonNode root) { 21 | return geometryFactory.createMultiLineString( 22 | lineStringsFromJson(root.get(COORDINATES))); 23 | } 24 | 25 | private LineString[] lineStringsFromJson(JsonNode array) { 26 | LineString[] strings = new LineString[array.size()]; 27 | for (int i = 0; i != array.size(); ++i) { 28 | strings[i] = geometryFactory.createLineString(PointParser.coordinatesFromJson(array.get(i))); 29 | } 30 | return strings; 31 | } 32 | 33 | @Override 34 | public MultiLineString geometryFromJson(JsonNode node) throws JsonMappingException { 35 | return multiLineStringFromJson(node); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/MultiPointParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.Geometry; 6 | import org.locationtech.jts.geom.GeometryFactory; 7 | import org.locationtech.jts.geom.MultiPoint; 8 | 9 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.COORDINATES; 10 | 11 | /** 12 | * Created by mihaildoronin on 11/11/15. 13 | */ 14 | public class MultiPointParser extends BaseParser implements GeometryParser { 15 | 16 | public MultiPointParser(GeometryFactory geometryFactory) { 17 | super(geometryFactory); 18 | } 19 | 20 | public MultiPoint multiPointFromJson(JsonNode root) { 21 | return geometryFactory.createMultiPoint( 22 | PointParser.coordinatesFromJson(root.get(COORDINATES))); 23 | } 24 | 25 | @Override 26 | public MultiPoint geometryFromJson(JsonNode node) throws JsonMappingException { 27 | return multiPointFromJson(node); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/MultiPolygonParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.GeometryFactory; 6 | import org.locationtech.jts.geom.MultiPolygon; 7 | import org.locationtech.jts.geom.Polygon; 8 | 9 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.COORDINATES; 10 | 11 | /** 12 | * Created by mihaildoronin on 11/11/15. 13 | */ 14 | public class MultiPolygonParser extends BaseParser implements GeometryParser { 15 | 16 | private PolygonParser helperParser; 17 | public MultiPolygonParser(GeometryFactory geometryFactory) { 18 | super(geometryFactory); 19 | helperParser = new PolygonParser(geometryFactory); 20 | } 21 | 22 | public MultiPolygon multiPolygonFromJson(JsonNode root) { 23 | JsonNode arrayOfPolygons = root.get(COORDINATES); 24 | return geometryFactory.createMultiPolygon(polygonsFromJson(arrayOfPolygons)); 25 | } 26 | 27 | private Polygon[] polygonsFromJson(JsonNode arrayOfPolygons) { 28 | Polygon[] polygons = new Polygon[arrayOfPolygons.size()]; 29 | for (int i = 0; i != arrayOfPolygons.size(); ++i) { 30 | polygons[i] = helperParser.polygonFromJsonArrayOfRings(arrayOfPolygons.get(i)); 31 | } 32 | return polygons; 33 | } 34 | 35 | @Override 36 | public MultiPolygon geometryFromJson(JsonNode node) throws JsonMappingException { 37 | return multiPolygonFromJson(node); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/PointParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.Coordinate; 6 | import org.locationtech.jts.geom.GeometryFactory; 7 | import org.locationtech.jts.geom.Point; 8 | 9 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.COORDINATES; 10 | 11 | /** 12 | * Created by mihaildoronin on 11/11/15. 13 | */ 14 | public class PointParser extends BaseParser implements GeometryParser { 15 | 16 | public PointParser(GeometryFactory geometryFactory) { 17 | super(geometryFactory); 18 | } 19 | 20 | public static Coordinate coordinateFromJson(JsonNode array) { 21 | assert array.isArray() && (array.size() == 2 || array.size() == 3) : "expecting coordinate array with single point [ x, y, |z| ]"; 22 | 23 | if (array.size() == 2) { 24 | return new Coordinate( 25 | array.get(0).asDouble(), 26 | array.get(1).asDouble()); 27 | } 28 | 29 | return new Coordinate( 30 | array.get(0).asDouble(), 31 | array.get(1).asDouble(), 32 | array.get(2).asDouble()); 33 | } 34 | 35 | public static Coordinate[] coordinatesFromJson(JsonNode array) { 36 | Coordinate[] points = new Coordinate[array.size()]; 37 | for (int i = 0; i != array.size(); ++i) { 38 | points[i] = PointParser.coordinateFromJson(array.get(i)); 39 | } 40 | return points; 41 | } 42 | 43 | public Point pointFromJson(JsonNode node) { 44 | return geometryFactory.createPoint( 45 | coordinateFromJson(node.get(COORDINATES))); 46 | } 47 | 48 | @Override 49 | public Point geometryFromJson(JsonNode node) throws JsonMappingException { 50 | return pointFromJson(node); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/parsers/PolygonParser.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.parsers; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import org.locationtech.jts.geom.*; 6 | 7 | import static com.bedatadriven.jackson.datatype.jts.GeoJson.COORDINATES; 8 | 9 | /** 10 | * Created by mihaildoronin on 11/11/15. 11 | */ 12 | public class PolygonParser extends BaseParser implements GeometryParser { 13 | 14 | public PolygonParser(GeometryFactory geometryFactory) { 15 | super(geometryFactory); 16 | } 17 | 18 | public Polygon polygonFromJson(JsonNode node) { 19 | JsonNode arrayOfRings = node.get(COORDINATES); 20 | return polygonFromJsonArrayOfRings(arrayOfRings); 21 | } 22 | 23 | public Polygon polygonFromJsonArrayOfRings(JsonNode arrayOfRings) { 24 | LinearRing shell = linearRingsFromJson(arrayOfRings.get(0)); 25 | int size = arrayOfRings.size(); 26 | LinearRing[] holes = new LinearRing[size - 1]; 27 | for (int i = 1; i < size; i++) { 28 | holes[i - 1] = linearRingsFromJson(arrayOfRings.get(i)); 29 | } 30 | return geometryFactory.createPolygon(shell, holes); 31 | } 32 | 33 | private LinearRing linearRingsFromJson(JsonNode coordinates) { 34 | assert coordinates.isArray() : "expected coordinates array"; 35 | return geometryFactory.createLinearRing(PointParser.coordinatesFromJson(coordinates)); 36 | } 37 | 38 | 39 | @Override 40 | public Polygon geometryFromJson(JsonNode node) throws JsonMappingException { 41 | return polygonFromJson(node); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/java/com/bedatadriven/jackson/datatype/jts/serialization/GeometryDeserializer.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts.serialization; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.parsers.GeometryParser; 4 | import com.fasterxml.jackson.core.JsonParser; 5 | import com.fasterxml.jackson.core.ObjectCodec; 6 | import com.fasterxml.jackson.databind.DeserializationContext; 7 | import com.fasterxml.jackson.databind.JsonDeserializer; 8 | import com.fasterxml.jackson.databind.JsonNode; 9 | import org.locationtech.jts.geom.Geometry; 10 | 11 | import java.io.IOException; 12 | 13 | /** 14 | * Created by mihaildoronin on 11/11/15. 15 | */ 16 | public class GeometryDeserializer extends JsonDeserializer { 17 | 18 | private GeometryParser geometryParser; 19 | 20 | public GeometryDeserializer(GeometryParser geometryParser) { 21 | this.geometryParser = geometryParser; 22 | } 23 | 24 | @Override 25 | public T deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { 26 | ObjectCodec oc = jsonParser.getCodec(); 27 | JsonNode root = oc.readTree(jsonParser); 28 | return geometryParser.geometryFromJson(root); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/resources/META-INF/LICENSE: -------------------------------------------------------------------------------- 1 | This copy of Jackson-datatype-jts module is licensed under the 2 | Apache (Software) License, version 2.0 ("the License"). 3 | See the License for details about distribution rights, and the 4 | specific rights regarding derivate works. 5 | 6 | You may obtain a copy of the License at: 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/main/resources/META-INF/services/com.fasterxml.jackson.databind.Module: -------------------------------------------------------------------------------- 1 | com.bedatadriven.jackson.datatype.jts.JtsModule -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/BaseJtsModuleTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.databind.ObjectWriter; 5 | import com.vividsolutions.jts.geom.Geometry; 6 | import com.vividsolutions.jts.geom.GeometryFactory; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.io.IOException; 11 | 12 | import static org.hamcrest.CoreMatchers.equalTo; 13 | import static org.hamcrest.core.Is.is; 14 | import static org.junit.Assert.assertThat; 15 | 16 | /** 17 | * Created by mihaildoronin on 11/11/15. 18 | */ 19 | public abstract class BaseJtsModuleTest { 20 | protected GeometryFactory gf = new GeometryFactory(); 21 | private ObjectWriter writer; 22 | private ObjectMapper mapper; 23 | private T geometry; 24 | private String geometryAsGeoJson; 25 | 26 | protected BaseJtsModuleTest() { 27 | } 28 | 29 | @Before 30 | public void setup() { 31 | mapper = new ObjectMapper(); 32 | mapper.registerModule(new JtsModule()); 33 | writer = mapper.writer(); 34 | geometry = createGeometry(); 35 | geometryAsGeoJson = createGeometryAsGeoJson(); 36 | } 37 | 38 | protected abstract Class getType(); 39 | 40 | protected abstract String createGeometryAsGeoJson(); 41 | 42 | protected abstract T createGeometry(); 43 | 44 | 45 | @Test 46 | public void shouldDeserializeConcreteType() throws Exception { 47 | T concreteGeometry = mapper.readValue(geometryAsGeoJson, getType()); 48 | assertThat( 49 | toJson(concreteGeometry), 50 | equalTo(geometryAsGeoJson)); 51 | } 52 | 53 | @Test 54 | public void shouldDeserializeAsInterface() throws Exception { 55 | assertRoundTrip(geometry); 56 | assertThat( 57 | toJson(geometry), 58 | equalTo(geometryAsGeoJson)); 59 | } 60 | 61 | protected String toJson(Object value) throws IOException { 62 | return writer.writeValueAsString(value); 63 | } 64 | 65 | protected void assertRoundTrip(T geom) throws IOException { 66 | String json = writer.writeValueAsString(geom); 67 | System.out.println(json); 68 | Geometry regeom = mapper.reader(Geometry.class).readValue(json); 69 | assertThat(geom.equalsExact(regeom), is(true)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/D3DPointTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.Point; 6 | 7 | import static org.hamcrest.CoreMatchers.equalTo; 8 | import static org.junit.Assert.assertThat; 9 | 10 | /** 11 | * @author lainard on 28/06/16. 12 | */ 13 | public class D3DPointTest extends BaseJtsModuleTest { 14 | 15 | @Override 16 | protected Class getType() { 17 | return Point.class; 18 | } 19 | 20 | @Override 21 | protected String createGeometryAsGeoJson() { 22 | return "{\"type\":\"Point\",\"coordinates\":[1.2345678,2.3456789,200.0]}"; 23 | } 24 | 25 | @Override 26 | protected Point createGeometry() { 27 | return gf.createPoint(new Coordinate(1.2345678, 2.3456789,200.0)); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/GeometryCollectionParserTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.GeometryCollection; 6 | 7 | /** 8 | * Created by mihaildoronin on 11/11/15. 9 | */ 10 | public class GeometryCollectionParserTest extends BaseJtsModuleTest { 11 | @Override 12 | protected Class getType() { 13 | return GeometryCollection.class; 14 | } 15 | 16 | @Override 17 | protected String createGeometryAsGeoJson() { 18 | return "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[1.2345678,2.3456789]}]}"; 19 | } 20 | 21 | @Override 22 | protected GeometryCollection createGeometry() { 23 | return gf.createGeometryCollection(new Geometry[] { 24 | gf.createPoint(new Coordinate(1.2345678, 2.3456789)) }); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/JtsModuleTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.fasterxml.jackson.databind.JsonMappingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import com.fasterxml.jackson.databind.ObjectWriter; 6 | import com.vividsolutions.jts.geom.*; 7 | import org.easymock.EasyMock; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import java.io.IOException; 12 | 13 | import static org.hamcrest.CoreMatchers.equalTo; 14 | import static org.junit.Assert.assertThat; 15 | 16 | public class JtsModuleTest { 17 | private GeometryFactory gf = new GeometryFactory(); 18 | private ObjectMapper mapper; 19 | @Before 20 | public void setupMapper() { 21 | 22 | mapper = new ObjectMapper(); 23 | mapper.registerModule(new JtsModule()); 24 | } 25 | 26 | @Test(expected = JsonMappingException.class) 27 | public void invalidGeometryType() throws IOException { 28 | String json = "{\"type\":\"Singularity\",\"coordinates\":[]}"; 29 | mapper.readValue(json, Geometry.class); 30 | } 31 | 32 | @Test(expected = JsonMappingException.class) 33 | public void unsupportedGeometry() throws IOException { 34 | Geometry unsupportedGeometry = EasyMock.createNiceMock("NonEuclideanGeometry", Geometry.class); 35 | EasyMock.replay(unsupportedGeometry); 36 | 37 | mapper.writeValue(System.out, unsupportedGeometry); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/LineStringTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.fasterxml.jackson.databind.deser.Deserializers; 4 | import com.vividsolutions.jts.geom.Coordinate; 5 | import com.vividsolutions.jts.geom.Geometry; 6 | import com.vividsolutions.jts.geom.LineString; 7 | 8 | /** 9 | * Created by mihaildoronin on 11/11/15. 10 | */ 11 | public class LineStringTest extends BaseJtsModuleTest { 12 | @Override 13 | protected Class getType() { 14 | return LineString.class; 15 | } 16 | 17 | @Override 18 | protected String createGeometryAsGeoJson() { 19 | return "{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,1.0]]}"; 20 | } 21 | 22 | @Override 23 | protected LineString createGeometry() { 24 | return gf.createLineString(new Coordinate[] { 25 | new Coordinate(100.0, 0.0), new Coordinate(101.0, 1.0) }); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/MultiLineStringTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.LineString; 6 | import com.vividsolutions.jts.geom.MultiLineString; 7 | 8 | /** 9 | * Created by mihaildoronin on 11/11/15. 10 | */ 11 | public class MultiLineStringTest extends BaseJtsModuleTest { 12 | @Override 13 | protected Class getType() { 14 | return MultiLineString.class; 15 | } 16 | 17 | @Override 18 | protected String createGeometryAsGeoJson() { 19 | return "{\"type\":\"MultiLineString\",\"coordinates\":[[[100.0,0.0],[101.0,1.0]],[[102.0,2.0],[103.0,3.0]]]}"; 20 | } 21 | 22 | @Override 23 | protected MultiLineString createGeometry() { 24 | return gf 25 | .createMultiLineString(new LineString[] { 26 | gf.createLineString(new Coordinate[] { 27 | new Coordinate(100.0, 0.0), 28 | new Coordinate(101.0, 1.0) }), 29 | gf.createLineString(new Coordinate[] { 30 | new Coordinate(102.0, 2.0), 31 | new Coordinate(103.0, 3.0) }) }); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/MultiPointTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.MultiPoint; 6 | import com.vividsolutions.jts.geom.Point; 7 | 8 | /** 9 | * Created by mihaildoronin on 11/11/15. 10 | */ 11 | public class MultiPointTest extends BaseJtsModuleTest { 12 | @Override 13 | protected Class getType() { 14 | return MultiPoint.class; 15 | } 16 | 17 | @Override 18 | protected String createGeometryAsGeoJson() { 19 | return "{\"type\":\"MultiPoint\",\"coordinates\":[[1.2345678,2.3456789]]}"; 20 | } 21 | 22 | @Override 23 | protected MultiPoint createGeometry() { 24 | return gf.createMultiPoint(new Point[] {gf 25 | .createPoint(new Coordinate(1.2345678, 2.3456789))}); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/MultiPolygonTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.fasterxml.jackson.databind.deser.Deserializers; 4 | import com.fasterxml.jackson.databind.node.BaseJsonNode; 5 | import com.vividsolutions.jts.geom.*; 6 | 7 | /** 8 | * Created by mihaildoronin on 11/11/15. 9 | */ 10 | public class MultiPolygonTest extends BaseJtsModuleTest { 11 | @Override 12 | protected Class getType() { 13 | return MultiPolygon.class; 14 | } 15 | 16 | @Override 17 | protected String createGeometryAsGeoJson() { 18 | return "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]]]}"; 19 | } 20 | 21 | @Override 22 | protected MultiPolygon createGeometry() { 23 | LinearRing shell = gf.createLinearRing(new Coordinate[] { 24 | new Coordinate(102.0, 2.0), new Coordinate(103.0, 2.0), 25 | new Coordinate(103.0, 3.0), new Coordinate(102.0, 3.0), 26 | new Coordinate(102.0, 2.0) }); 27 | return gf.createMultiPolygon(new Polygon[] { gf 28 | .createPolygon(shell, null) }); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/PointTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.Point; 6 | 7 | import static org.hamcrest.CoreMatchers.equalTo; 8 | import static org.junit.Assert.assertThat; 9 | 10 | /** 11 | * Created by mihaildoronin on 11/11/15. 12 | */ 13 | public class PointTest extends BaseJtsModuleTest { 14 | @Override 15 | protected Class getType() { 16 | return Point.class; 17 | } 18 | 19 | @Override 20 | protected String createGeometryAsGeoJson() { 21 | return "{\"type\":\"Point\",\"coordinates\":[1.2345678,2.3456789]}"; 22 | } 23 | 24 | @Override 25 | protected Point createGeometry() { 26 | return gf.createPoint(new Coordinate(1.2345678, 2.3456789)); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/PolygonTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.LinearRing; 6 | import com.vividsolutions.jts.geom.Polygon; 7 | 8 | /** 9 | * Created by mihaildoronin on 11/11/15. 10 | */ 11 | public class PolygonTest extends BaseJtsModuleTest { 12 | @Override 13 | protected Class getType() { 14 | return Polygon.class; 15 | } 16 | 17 | @Override 18 | protected String createGeometryAsGeoJson() { 19 | return "{\"type\":\"Polygon\",\"coordinates\":[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]]}"; 20 | } 21 | 22 | @Override 23 | protected Polygon createGeometry() { 24 | LinearRing shell = gf.createLinearRing(new Coordinate[] { 25 | new Coordinate(102.0, 2.0), new Coordinate(103.0, 2.0), 26 | new Coordinate(103.0, 3.0), new Coordinate(102.0, 3.0), 27 | new Coordinate(102.0, 2.0) }); 28 | LinearRing[] holes = new LinearRing[0]; 29 | return gf.createPolygon(shell, holes); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /00-jackson-datatype-jts/src/test/java/com/bedatadriven/jackson/datatype/jts/PolygonWithHolesTest.java: -------------------------------------------------------------------------------- 1 | package com.bedatadriven.jackson.datatype.jts; 2 | 3 | import com.vividsolutions.jts.geom.Coordinate; 4 | import com.vividsolutions.jts.geom.Geometry; 5 | import com.vividsolutions.jts.geom.LinearRing; 6 | import com.vividsolutions.jts.geom.Polygon; 7 | 8 | import static org.junit.Assert.assertThat; 9 | 10 | /** 11 | * Created by mihaildoronin on 11/11/15. 12 | */ 13 | public class PolygonWithHolesTest extends BaseJtsModuleTest { 14 | @Override 15 | protected Class getType() { 16 | return Polygon.class; 17 | } 18 | 19 | @Override 20 | protected String createGeometryAsGeoJson() { 21 | return "{\"type\":\"Polygon\",\"coordinates\":[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]],[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}"; 22 | } 23 | 24 | @Override 25 | protected Polygon createGeometry() { 26 | LinearRing shell = gf.createLinearRing(new Coordinate[] { 27 | new Coordinate(102.0, 2.0), new Coordinate(103.0, 2.0), 28 | new Coordinate(103.0, 3.0), new Coordinate(102.0, 3.0), 29 | new Coordinate(102.0, 2.0) }); 30 | LinearRing[] holes = new LinearRing[] { gf 31 | .createLinearRing(new Coordinate[] { 32 | new Coordinate(100.2, 0.2), new Coordinate(100.8, 0.2), 33 | new Coordinate(100.8, 0.8), new Coordinate(100.2, 0.8), 34 | new Coordinate(100.2, 0.2) }) }; 35 | return gf.createPolygon(shell, holes); 36 | } 37 | } -------------------------------------------------------------------------------- /00-postgis-jdbc-jts/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | 3 | group = 'xyz.lonelyleaf' 4 | version = '2.3.1-SNAPSHOT' 5 | 6 | sourceCompatibility = 1.8 7 | targetCompatibility = 1.8 8 | 9 | ext { 10 | dependencyTestngVersion = "6.14.3" 11 | dependencySlfjVersion = "1.7.25" 12 | dependencyLogbackVersion = "1.2.3" 13 | } 14 | 15 | configurations { 16 | compileOnly { 17 | extendsFrom annotationProcessor 18 | } 19 | } 20 | 21 | dependencies { 22 | api "org.postgresql:postgresql:$postgresJdbcVersion" 23 | api "net.postgis:postgis-jdbc:$postgisJdbcVersion" 24 | //jts 25 | api "org.locationtech.jts:jts-core:$jtsVersion" 26 | api "org.locationtech.spatial4j:spatial4j:$spatial4jVersion" 27 | 28 | testCompile "ch.qos.logback:logback-classic:$dependencyLogbackVersion" 29 | testCompile "ch.qos.logback:logback-core:$dependencyLogbackVersion" 30 | testCompile "org.slf4j:slf4j-api:$dependencySlfjVersion" 31 | testCompile "org.testng:testng:$dependencyTestngVersion" 32 | } 33 | 34 | test { 35 | useJUnitPlatform() 36 | } 37 | -------------------------------------------------------------------------------- /00-postgis-jdbc-jts/src/main/java/org/postgis/jts/JtsGeometry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JtsGeometry.java 3 | * 4 | * Wrapper for PostgreSQL JDBC driver to allow transparent reading and writing 5 | * of JTS geometries 6 | * 7 | * (C) 2005 Markus Schaber, markus.schaber@logix-tt.com 8 | * 9 | * (C) 2015 Phillip Ross, phillip.w.g.ross@gmail.com 10 | * 11 | * This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this library; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | */ 26 | 27 | package org.postgis.jts; 28 | 29 | import org.locationtech.jts.geom.*; 30 | import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory; 31 | import org.locationtech.jts.io.WKTReader; 32 | import org.postgresql.util.PGobject; 33 | 34 | import java.sql.SQLException; 35 | 36 | /** 37 | * JTS Geometry SQL wrapper. Supports PostGIS 1.x (lwgeom hexwkb) for writing 38 | * and both PostGIS 0.x (EWKT) and 1.x (lwgeom hexwkb) for reading. 39 | * 40 | * @author Markus Schaber 41 | */ 42 | 43 | public class JtsGeometry extends PGobject { 44 | /* JDK 1.5 Serialization */ 45 | private static final long serialVersionUID = 0x100; 46 | 47 | Geometry geom; 48 | 49 | final static JtsBinaryParser bp = new JtsBinaryParser(); 50 | 51 | final static JtsBinaryWriter bw = new JtsBinaryWriter(); 52 | 53 | final static PrecisionModel prec = new PrecisionModel(); 54 | 55 | final static CoordinateSequenceFactory csfac = PackedCoordinateSequenceFactory.DOUBLE_FACTORY; 56 | 57 | final static GeometryFactory geofac = new GeometryFactory(prec, 0, csfac); 58 | 59 | static final WKTReader reader = new WKTReader(geofac); 60 | 61 | /** Constructor called by JDBC drivers */ 62 | public JtsGeometry() { 63 | setType("geometry"); 64 | } 65 | 66 | public JtsGeometry(Geometry geom) { 67 | this(); 68 | this.geom = geom; 69 | } 70 | 71 | public JtsGeometry(String value) throws SQLException { 72 | this(); 73 | setValue(value); 74 | } 75 | 76 | public void setValue(String value) throws SQLException { 77 | geom = geomFromString(value); 78 | } 79 | 80 | public static Geometry geomFromString(String value) throws SQLException { 81 | try { 82 | value = value.trim(); 83 | if (value.startsWith("00") || value.startsWith("01")) { 84 | return bp.parse(value); 85 | } else { 86 | Geometry result; 87 | // no srid := 0 in JTS world 88 | int srid = 0; 89 | // break up geometry into srid and wkt 90 | if (value.startsWith("SRID=")) { 91 | String[] temp = value.split(";"); 92 | value = temp[1].trim(); 93 | srid = Integer.parseInt(temp[0].substring(5)); 94 | } 95 | 96 | result = reader.read(value); 97 | setSridRecurse(result, srid); 98 | return result; 99 | } 100 | } catch (Exception E) { 101 | E.printStackTrace(); 102 | throw new SQLException("Error parsing SQL data:" + E); 103 | } 104 | } 105 | 106 | 107 | /** 108 | * Recursively set a srid for the geometry and all subgeometries 109 | * @param geom Geometry to work on 110 | * @param srid SRID to be set to 111 | */ 112 | public static void setSridRecurse(final Geometry geom, final int srid) { 113 | geom.setSRID(srid); 114 | if (geom instanceof GeometryCollection) { 115 | final int subcnt = geom.getNumGeometries(); 116 | for (int i = 0; i < subcnt; i++) { 117 | setSridRecurse(geom.getGeometryN(i), srid); 118 | } 119 | } else if (geom instanceof Polygon) { 120 | Polygon poly = (Polygon) geom; 121 | poly.getExteriorRing().setSRID(srid); 122 | final int subcnt = poly.getNumInteriorRing(); 123 | for (int i = 0; i < subcnt; i++) { 124 | poly.getInteriorRingN(i).setSRID(srid); 125 | } 126 | } 127 | } 128 | 129 | public Geometry getGeometry() { 130 | return geom; 131 | } 132 | 133 | public String toString() { 134 | return geom.toString(); 135 | } 136 | 137 | public String getValue() { 138 | return bw.writeHexed(getGeometry()); 139 | } 140 | 141 | public Object clone() { 142 | JtsGeometry obj = new JtsGeometry(geom); 143 | obj.setType(type); 144 | return obj; 145 | } 146 | 147 | public boolean equals(Object obj) { 148 | if ((obj != null) && (obj instanceof JtsGeometry)) { 149 | Geometry other = ((JtsGeometry) obj).geom; 150 | if (this.geom == other) { // handles identity as well as both 151 | // ==null 152 | return true; 153 | } else if (this.geom != null && other != null) { 154 | return other.equals(this.geom); 155 | } 156 | } 157 | return false; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /00-postgis-jdbc-jts/src/main/java/org/postgis/jts/JtsGisWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JtsGisWrapper.java 3 | * 4 | * Allows transparent usage of JTS Geometry classes via PostgreSQL JDBC driver 5 | * connected to a PostGIS enabled PostgreSQL server. 6 | * 7 | * (C) 2005 Markus Schaber, markus.schaber@logix-tt.com 8 | * 9 | * (C) 2015 Phillip Ross, phillip.w.g.ross@gmail.com 10 | * 11 | * This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this library; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | */ 26 | 27 | package org.postgis.jts; 28 | 29 | import org.postgresql.Driver; 30 | import org.postgresql.PGConnection; 31 | 32 | import java.sql.Connection; 33 | import java.sql.SQLException; 34 | import java.util.Properties; 35 | 36 | /** 37 | * JtsGisWrapper 38 | * 39 | * Wraps the PostGreSQL Driver to add the JTS/PostGIS Object Classes. 40 | * 41 | * This method currently works with J2EE DataSource implementations, and with 42 | * DriverManager framework. 43 | * 44 | * Simply replace the "jdbc:postgresql:" with a "jdbc:postgresql_JTS" in the 45 | * jdbc URL. 46 | * 47 | * @author markus.schaber@logix-tt.com 48 | * 49 | */ 50 | public class JtsGisWrapper extends Driver { 51 | 52 | private static final String POSTGRES_PROTOCOL = "jdbc:postgresql:"; 53 | private static final String POSTGIS_PROTOCOL = "jdbc:postgresql_JTS:"; 54 | public static final String REVISION = "$Revision$"; 55 | 56 | public JtsGisWrapper() { 57 | super(); 58 | } 59 | 60 | static { 61 | try { 62 | // Analogy to org.postgresql.Driver 63 | java.sql.DriverManager.registerDriver(new JtsGisWrapper()); 64 | } catch (SQLException e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | 69 | /** 70 | * Creates a postgresql connection, and then adds the PostGIS data types to 71 | * it calling addpgtypes() 72 | * 73 | * @param url the URL of the database to connect to 74 | * @param info a list of arbitrary tag/value pairs as connection arguments 75 | * @return a connection to the URL or null if it isnt us 76 | * @exception SQLException if a database access error occurs 77 | * 78 | * @see java.sql.Driver#connect 79 | * @see Driver 80 | */ 81 | public Connection connect(String url, Properties info) throws SQLException { 82 | url = mangleURL(url); 83 | Connection result = super.connect(url, info); 84 | addGISTypes((PGConnection) result); 85 | return result; 86 | } 87 | 88 | /** 89 | * Adds the JTS/PostGIS Data types to a PG Connection. 90 | * @param pgconn The PGConnection object to add the types to 91 | * @throws SQLException when an SQLException occurs 92 | */ 93 | public static void addGISTypes(PGConnection pgconn) throws SQLException { 94 | pgconn.addDataType("geometry", JtsGeometry.class); 95 | pgconn.addDataType("geography", JtsGeometry.class); 96 | pgconn.addDataType("box3d", org.postgis.PGbox3d.class); 97 | pgconn.addDataType("box2d", org.postgis.PGbox2d.class); 98 | } 99 | 100 | /** 101 | * Mangles the PostGIS URL to return the original PostGreSQL URL 102 | * 103 | * @param url String containing the url to be "mangled" 104 | * @return "mangled" string 105 | * @throws SQLException when a SQLException occurs 106 | */ 107 | public static String mangleURL(String url) throws SQLException { 108 | if (url.startsWith(POSTGIS_PROTOCOL)) { 109 | return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length()); 110 | } else { 111 | throw new SQLException("Unknown protocol or subprotocol in url " + url); 112 | } 113 | } 114 | 115 | /** 116 | * Returns true if the driver thinks it can open a connection to the given 117 | * URL. Typically, drivers will return true if they understand the 118 | * subprotocol specified in the URL and false if they don't. Our protocols 119 | * start with jdbc:postgresql_postGIS: 120 | * 121 | * @see java.sql.Driver#acceptsURL 122 | * @param url the URL of the driver 123 | * @return true if this driver accepts the given URL 124 | */ 125 | public boolean acceptsURL(String url) { 126 | try { 127 | url = mangleURL(url); 128 | } catch (SQLException e) { 129 | return false; 130 | } 131 | return super.acceptsURL(url); 132 | } 133 | 134 | /** 135 | * Gets the underlying drivers major version number 136 | * 137 | * @return the drivers major version number 138 | */ 139 | 140 | public int getMajorVersion() { 141 | return super.getMajorVersion(); 142 | } 143 | 144 | /** 145 | * Get the underlying drivers minor version number 146 | * 147 | * @return the drivers minor version number 148 | */ 149 | public int getMinorVersion() { 150 | return super.getMinorVersion(); 151 | } 152 | 153 | /** 154 | * Returns our own CVS version plus postgres Version 155 | * 156 | * @return String representation of the version 157 | */ 158 | public static String getVersion() { 159 | return "JtsGisWrapper " + REVISION + ", wrapping " + Driver.getVersion(); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /00-postgis-jdbc-jts/src/main/javadoc/overview.html: -------------------------------------------------------------------------------- 1 |

Parser between JTS and PostGIS geometry formats.

-------------------------------------------------------------------------------- /00-postgis-jdbc-jts/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /00-postgis-jdbc-jts/src/test/resources/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /01-postgis/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'org.springframework.boot' 3 | apply plugin: 'io.spring.dependency-management' 4 | apply plugin: "io.freefair.lombok" 5 | 6 | group = 'xyz.lonelyleaf' 7 | version = '0.0.1-SNAPSHOT' 8 | sourceCompatibility = '11' 9 | 10 | configurations { 11 | compileOnly { 12 | extendsFrom annotationProcessor 13 | } 14 | } 15 | 16 | //添加mapstuct的一些配置 17 | compileJava { 18 | options.compilerArgs = [ 19 | '-Amapstruct.defaultComponentModel=spring',//默认生成为spring bean 20 | '-Amapstruct.unmappedTargetPolicy=ERROR'//未映射字段默认报错 21 | ] 22 | } 23 | 24 | dependencies { 25 | implementation "org.springframework.boot:spring-boot-starter" 26 | implementation 'org.springframework.boot:spring-boot-starter-web' 27 | implementation "org.springframework.boot:spring-boot-starter-actuator" 28 | 29 | //mybatis 30 | implementation "com.baomidou:mybatis-plus-boot-starter:$mybatisPlusVersion" 31 | //数据库管理 32 | implementation 'org.flywaydb:flyway-core' 33 | //pg驱动 34 | implementation "org.postgresql:postgresql:$postgresJdbcVersion" 35 | //postgis 36 | implementation "net.postgis:postgis-jdbc:$postgisJdbcVersion" 37 | 38 | //jackson相关 39 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-json-org" 40 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" 41 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" 42 | //implementation 'com.fasterxml.jackson.datatype:jackson-datatype-joda' 43 | implementation "com.fasterxml.jackson.core:jackson-annotations" 44 | implementation "com.fasterxml.jackson.core:jackson-databind" 45 | implementation "com.fasterxml.jackson.module:jackson-module-parameter-names" 46 | implementation "com.fasterxml.jackson.module:jackson-module-afterburner" 47 | 48 | //java bean转换 49 | implementation "org.mapstruct:mapstruct-jdk8:$mapstructVersion" 50 | annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion" 51 | 52 | //swagger文档 53 | implementation("io.springfox:springfox-swagger2:$springFoxVersion") { 54 | exclude group: 'io.swagger', module: 'swagger-annotations' 55 | exclude group: 'io.swagger', module: 'swagger-models' 56 | } 57 | implementation "io.springfox:springfox-swagger-ui:$springFoxVersion" 58 | //添加下面依赖是为了排除获取api文档时的报错:java.lang.NumberFormatException: For input string: "" 59 | //后面升级springfox后,可以移除下面的两个依赖 60 | implementation 'io.swagger:swagger-annotations:1.5.21' 61 | implementation 'io.swagger:swagger-models:1.5.21' 62 | 63 | testImplementation('org.springframework.boot:spring-boot-starter-test') { 64 | exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' 65 | } 66 | } 67 | 68 | test { 69 | useJUnitPlatform() 70 | } 71 | -------------------------------------------------------------------------------- /01-postgis/img/data-output1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/data-output1.png -------------------------------------------------------------------------------- /01-postgis/img/data-output2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/data-output2.png -------------------------------------------------------------------------------- /01-postgis/img/pgadmin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/pgadmin.png -------------------------------------------------------------------------------- /01-postgis/img/postgis安装.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/postgis安装.png -------------------------------------------------------------------------------- /01-postgis/img/query-tool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/query-tool.png -------------------------------------------------------------------------------- /01-postgis/img/view-table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/view-table.png -------------------------------------------------------------------------------- /01-postgis/img/新建数据库.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/01-postgis/img/新建数据库.png -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/GisDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GisDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GisDemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/config/JacksonConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 5 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 6 | import com.fasterxml.jackson.module.afterburner.AfterburnerModule; 7 | import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import com.github.lonelyleaf.gis.util.JsonUtil; 11 | 12 | /** 13 | * jackson配置 14 | */ 15 | @Configuration 16 | public class JacksonConfig { 17 | 18 | /* 19 | * Jackson Afterburner module to speed up serialization/deserialization. 20 | */ 21 | @Bean 22 | public AfterburnerModule afterburnerModule() { 23 | AfterburnerModule module = new AfterburnerModule(); 24 | // make Afterburner generate bytecode only for public getters/setter and fields 25 | // without this, Java 9+ complains of "Illegal reflective access" 26 | module.setUseValueClassLoader(false); 27 | return module; 28 | } 29 | 30 | @Bean 31 | public ParameterNamesModule parameterNamesModule() { 32 | return new ParameterNamesModule(); 33 | } 34 | 35 | @Bean 36 | public Jdk8Module jdk8Module() { 37 | return new Jdk8Module(); 38 | } 39 | 40 | @Bean 41 | public JavaTimeModule javaTimeModule() { 42 | return new JavaTimeModule(); 43 | } 44 | 45 | @Bean 46 | public JsonUtil jsonUtil(ObjectMapper objectMapper) { 47 | //只是把objectmapper注入 48 | JsonUtil.objectMapper = objectMapper; 49 | return new JsonUtil(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/config/MybatisPlusConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import com.baomidou.mybatisplus.extension.incrementer.PostgreKeyGenerator; 4 | import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @Configuration 10 | @MapperScan("com.github.lonelyleaf.gis.repo") 11 | public class MybatisPlusConfig { 12 | 13 | @Bean 14 | public PostgreKeyGenerator postgreKeyGenerator() { 15 | return new PostgreKeyGenerator(); 16 | } 17 | 18 | /** 19 | * 分页插件 20 | */ 21 | @Bean 22 | public PaginationInterceptor paginationInterceptor() { 23 | PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); 24 | paginationInterceptor.setLimit(-1); 25 | return paginationInterceptor; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.http.ResponseEntity; 9 | import springfox.documentation.builders.ApiInfoBuilder; 10 | import springfox.documentation.builders.PathSelectors; 11 | import springfox.documentation.builders.RequestHandlerSelectors; 12 | import springfox.documentation.service.ApiInfo; 13 | import springfox.documentation.spi.DocumentationType; 14 | import springfox.documentation.spring.web.plugins.Docket; 15 | import springfox.documentation.swagger.web.DocExpansion; 16 | import springfox.documentation.swagger.web.UiConfiguration; 17 | import springfox.documentation.swagger.web.UiConfigurationBuilder; 18 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 19 | 20 | /** 21 | * swagger配置,这里添加了基于jwt认证的代码 22 | */ 23 | @Configuration 24 | @EnableSwagger2 25 | @EnableConfigurationProperties(SwaggerProperties.class) 26 | @ConditionalOnProperty(value = "swagger.enable", matchIfMissing = true) 27 | public class SwaggerConfig { 28 | 29 | private final SwaggerProperties p; 30 | 31 | public SwaggerConfig(SwaggerProperties p) { 32 | this.p = p; 33 | } 34 | 35 | @Bean 36 | @ConditionalOnMissingBean(Docket.class) 37 | public Docket api() { 38 | Docket docket = new Docket(DocumentationType.SWAGGER_2); 39 | docket.select() 40 | .build() 41 | .pathMapping("/") 42 | .genericModelSubstitutes(ResponseEntity.class) 43 | .enableUrlTemplating(false); 44 | //@formatter:off 45 | return docket 46 | .apiInfo(apiInfo()) 47 | .useDefaultResponseMessages(false) 48 | .select() 49 | .paths(PathSelectors.any()) 50 | //基于包名显示文档 51 | .apis(RequestHandlerSelectors.basePackage(p.getBasePackage())) 52 | .build(); 53 | //@formatter:on 54 | } 55 | 56 | private ApiInfo apiInfo() { 57 | return new ApiInfoBuilder() 58 | .title("seed-demo api文档") 59 | .description("api文档描述,todo") 60 | .build(); 61 | } 62 | 63 | @Bean 64 | public UiConfiguration uiConfig() { 65 | return UiConfigurationBuilder.builder() 66 | .docExpansion(DocExpansion.NONE) 67 | .build(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/config/SwaggerProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * swagger配置参数 7 | */ 8 | @ConfigurationProperties(prefix = "swagger") 9 | public class SwaggerProperties { 10 | private Boolean enable = true; 11 | private String baseUrl = "/"; 12 | private String basePackage = "com.github.lonelyleaf.gis.rest"; 13 | 14 | public Boolean getEnable() { 15 | return enable; 16 | } 17 | 18 | public void setEnable(Boolean enable) { 19 | this.enable = enable; 20 | } 21 | 22 | public String getBaseUrl() { 23 | return baseUrl; 24 | } 25 | 26 | public void setBaseUrl(String baseUrl) { 27 | this.baseUrl = baseUrl; 28 | } 29 | 30 | public String getBasePackage() { 31 | return basePackage; 32 | } 33 | 34 | public void setBasePackage(String basePackage) { 35 | this.basePackage = basePackage; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/dto/GpsDto.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Data; 6 | 7 | import java.util.Date; 8 | 9 | @Data 10 | public class GpsDto { 11 | @JsonFormat(shape = JsonFormat.Shape.STRING, 12 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 13 | timezone = "+08") 14 | @ApiModelProperty("时间") 15 | private Date time; 16 | @ApiModelProperty("设备id") 17 | private String devId; 18 | @ApiModelProperty("位置") 19 | private SimplePoint location; 20 | @ApiModelProperty("卫星定位数") 21 | private int gpsNum; 22 | @ApiModelProperty("GPS定位信息") 23 | private String gpsType; 24 | @ApiModelProperty("对地真北航向角") 25 | private double azimuth; 26 | @ApiModelProperty("地面速率") 27 | private double gndRate; 28 | } 29 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/dto/SimplePoint.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class SimplePoint { 11 | 12 | private double x; 13 | private double y; 14 | 15 | public SimplePoint(org.postgis.Point point) { 16 | this.x = point.x; 17 | this.y = point.y; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/entity/GpsEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.entity; 2 | 3 | import com.baomidou.mybatisplus.annotation.TableName; 4 | import com.fasterxml.jackson.annotation.JsonFormat; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Data; 7 | import org.postgis.Point; 8 | 9 | import java.util.Date; 10 | 11 | @Data 12 | @TableName("t_gps") 13 | public class GpsEntity { 14 | @JsonFormat(shape = JsonFormat.Shape.STRING, 15 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 16 | timezone = "+08") 17 | @ApiModelProperty("时间") 18 | private Date time; 19 | @ApiModelProperty("设备id") 20 | private String devId; 21 | @ApiModelProperty("位置") 22 | private Point location; 23 | @ApiModelProperty("卫星定位数") 24 | private int gpsNum; 25 | @ApiModelProperty("GPS定位信息") 26 | private String gpsType; 27 | @ApiModelProperty("对地真北航向角") 28 | private double azimuth; 29 | @ApiModelProperty("地面速率") 30 | private double gndRate; 31 | } 32 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/mapper/GpsMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.mapper; 2 | 3 | 4 | import com.github.lonelyleaf.gis.dto.GpsDto; 5 | import com.github.lonelyleaf.gis.dto.SimplePoint; 6 | import com.github.lonelyleaf.gis.entity.GpsEntity; 7 | import org.postgis.Point; 8 | 9 | @org.mapstruct.Mapper 10 | public interface GpsMapper { 11 | 12 | GpsDto toDto(GpsEntity entity); 13 | 14 | GpsEntity toEntity(GpsDto entity); 15 | 16 | default SimplePoint toSimple(org.postgis.Point point) { 17 | return new SimplePoint(point); 18 | } 19 | 20 | default org.postgis.Point toGisPoint(SimplePoint point) { 21 | Point gisPoint = new Point(); 22 | gisPoint.x = point.getX(); 23 | gisPoint.y = point.getY(); 24 | gisPoint.dimension = 2; 25 | //WGS84坐标系,也就是GPS使用的坐标 26 | gisPoint.srid = 4326; 27 | return gisPoint; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/mybatis/postgis/AbstractGeometryTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.postgis; 18 | 19 | import org.apache.ibatis.type.BaseTypeHandler; 20 | import org.apache.ibatis.type.JdbcType; 21 | import org.postgis.Geometry; 22 | import org.postgis.PGgeometry; 23 | 24 | import java.sql.CallableStatement; 25 | import java.sql.PreparedStatement; 26 | import java.sql.ResultSet; 27 | import java.sql.SQLException; 28 | 29 | @SuppressWarnings("unchecked") 30 | public abstract class AbstractGeometryTypeHandler extends BaseTypeHandler { 31 | 32 | public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { 33 | PGgeometry geometry = new PGgeometry(); 34 | geometry.setGeometry(parameter); 35 | ps.setObject(i, geometry); 36 | } 37 | 38 | public T getNullableResult(ResultSet rs, String columnName) throws SQLException { 39 | PGgeometry pGgeometry = (PGgeometry) rs.getObject(columnName); 40 | if (pGgeometry == null) { 41 | return null; 42 | } 43 | return (T) pGgeometry.getGeometry(); 44 | } 45 | 46 | public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 47 | PGgeometry pGgeometry = (PGgeometry) rs.getObject(columnIndex); 48 | if (pGgeometry == null) { 49 | return null; 50 | } 51 | return (T) pGgeometry.getGeometry(); 52 | } 53 | 54 | public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 55 | PGgeometry pGgeometry = (PGgeometry) cs.getObject(columnIndex); 56 | if (pGgeometry == null) { 57 | return null; 58 | } 59 | return (T) pGgeometry.getGeometry(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/mybatis/postgis/LineStringTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.lonelyleaf.gis.mybatis.postgis; 17 | 18 | import org.apache.ibatis.type.MappedTypes; 19 | import org.postgis.LineString; 20 | 21 | @MappedTypes(LineString.class) 22 | public class LineStringTypeHandler extends AbstractGeometryTypeHandler { 23 | } 24 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/mybatis/postgis/MultiPointTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.postgis; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.postgis.MultiPoint; 21 | 22 | @MappedTypes(MultiPoint.class) 23 | public class MultiPointTypeHandler extends AbstractGeometryTypeHandler{ 24 | } 25 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/mybatis/postgis/PointTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.postgis; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.postgis.Point; 21 | 22 | @MappedTypes(Point.class) 23 | public class PointTypeHandler extends AbstractGeometryTypeHandler { 24 | } 25 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/mybatis/postgis/PolygonTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.postgis; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.postgis.Polygon; 21 | 22 | @MappedTypes(Polygon.class) 23 | public class PolygonTypeHandler extends AbstractGeometryTypeHandler { 24 | } 25 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/repo/GpsRepo.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.repo; 2 | 3 | 4 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 5 | import com.github.lonelyleaf.gis.entity.GpsEntity; 6 | 7 | /** 8 | * GPS相关 9 | */ 10 | public interface GpsRepo extends BaseMapper { 11 | } 12 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/rest/GpsController.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.rest; 2 | 3 | import com.github.lonelyleaf.gis.dto.GpsDto; 4 | import com.github.lonelyleaf.gis.dto.SimplePoint; 5 | import com.github.lonelyleaf.gis.entity.GpsEntity; 6 | import com.github.lonelyleaf.gis.mapper.GpsMapper; 7 | import com.github.lonelyleaf.gis.service.GpsService; 8 | import com.google.common.base.Strings; 9 | import org.springframework.format.annotation.DateTimeFormat; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | import java.util.Date; 13 | import java.util.List; 14 | import java.util.stream.Collectors; 15 | 16 | @RestController 17 | @RequestMapping("/gps") 18 | public class GpsController { 19 | 20 | private final GpsService gpsService; 21 | private final GpsMapper gpsMapper; 22 | 23 | public GpsController(GpsService gpsService, GpsMapper gpsMapper) { 24 | this.gpsService = gpsService; 25 | this.gpsMapper = gpsMapper; 26 | } 27 | 28 | @GetMapping("/history") 29 | public List history( 30 | @RequestParam(value = "devId", required = false) 31 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 32 | @RequestParam(value = "bTime", required = false) 33 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 34 | @RequestParam(value = "eTime", required = false) 35 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 36 | return gpsService.history(devId, bTime, eTime).stream() 37 | .map(gpsMapper::toDto) 38 | .collect(Collectors.toList()); 39 | } 40 | 41 | @GetMapping("/history/simple") 42 | public List simpleHistory( 43 | @RequestParam(value = "devId", required = false) 44 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 45 | @RequestParam(value = "bTime", required = false) 46 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 47 | @RequestParam(value = "eTime", required = false) 48 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 49 | List history = gpsService.history(devId, bTime, eTime); 50 | return history.stream() 51 | .map(gpsEntity -> new SimplePoint(gpsEntity.getLocation())) 52 | .collect(Collectors.toList()); 53 | } 54 | 55 | @PostMapping 56 | public String simpleHistory(@RequestBody GpsDto gpsDto) { 57 | if (gpsDto.getTime() == null) { 58 | gpsDto.setTime(new Date()); 59 | } 60 | if (Strings.isNullOrEmpty(gpsDto.getDevId())) { 61 | throw new IllegalArgumentException("devId不能为空"); 62 | } 63 | 64 | GpsEntity entity = gpsMapper.toEntity(gpsDto); 65 | gpsService.save(entity); 66 | return "ok"; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/service/GpsService.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.service; 2 | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 4 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 5 | import com.github.lonelyleaf.gis.entity.GpsEntity; 6 | import com.google.common.base.Strings; 7 | import org.springframework.stereotype.Service; 8 | import com.github.lonelyleaf.gis.repo.GpsRepo; 9 | 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | @Service 14 | public class GpsService extends ServiceImpl { 15 | 16 | 17 | public List history(String devId, Date bTime, Date eTime) { 18 | QueryWrapper wrapper = new QueryWrapper<>(); 19 | wrapper.orderByAsc("time"); 20 | if (!Strings.isNullOrEmpty(devId)) { 21 | wrapper.eq("dev_id", devId); 22 | } 23 | if (bTime != null) { 24 | wrapper.ge("time", bTime); 25 | } 26 | if (eTime != null) { 27 | wrapper.le("time", eTime); 28 | } 29 | 30 | return list(wrapper); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /01-postgis/src/main/java/com/github/lonelyleaf/gis/util/JsonUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.util; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | 6 | import java.io.IOException; 7 | 8 | /** 9 | * 方便序列化json 10 | */ 11 | public class JsonUtil { 12 | 13 | public static ObjectMapper objectMapper = new ObjectMapper() 14 | .registerModules(ObjectMapper.findModules()); 15 | 16 | public static String toJson(Object obj) { 17 | try { 18 | return objectMapper.writeValueAsString(obj); 19 | } catch (JsonProcessingException e) { 20 | throw new RuntimeException(e); 21 | } 22 | } 23 | 24 | public static String toPrettyJson(Object obj) { 25 | try { 26 | return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); 27 | } catch (JsonProcessingException e) { 28 | throw new RuntimeException(e); 29 | } 30 | } 31 | 32 | public static T fromJson(String s, Class clazz) { 33 | try { 34 | return objectMapper.readValue(s, clazz); 35 | } catch (IOException e) { 36 | throw new RuntimeException(e); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /01-postgis/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | jackson: 5 | serialization: 6 | write_dates_as_timestamps: true 7 | 8 | servlet: 9 | multipart: 10 | max-file-size: 10MB 11 | 12 | jdbc: 13 | template: 14 | query-timeout: 15 15 | 16 | datasource: 17 | # driver-class-name: org.postgresql.Driver 18 | # url: jdbc:postgresql://localhost:5432/gis-test 19 | #这里使用的驱动不一样!,可以自动添加gis的各种类型 20 | driver-class-name: com.github.lonelyleaf.gis.db.DriverWrapper 21 | #url也不一样 22 | url: jdbc:postgresql_postGIS://localhost:5432/gis-test 23 | username: postgres 24 | password: admin 25 | initialization-mode: never 26 | 27 | 28 | mybatis-plus: 29 | # configuration: 30 | # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 31 | type-handlers-package: "com.github.lonelyleaf.gis.mybatis.postgis" -------------------------------------------------------------------------------- /01-postgis/src/main/resources/db/migration/V1.0__init.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:e616588bfa1d6f6013a056ec9328bcea5d7b7071e993b462e1058a7e0e597b74 3 | size 708 4 | -------------------------------------------------------------------------------- /01-postgis/src/main/resources/db/migration/V1.1__gps_sample_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:c1af1aed4a980e272ca3eea7adaf8970dd2e9d38b9de91b90a1854207cdbc726 3 | size 1202496 4 | -------------------------------------------------------------------------------- /01-postgis/src/main/resources/db/migration/V1.2__index_gps.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:6251cacbe1fd5968f7b6c273cbf04887fd2e03fba99c3d0f39a0a11bd57497b1 3 | size 62 4 | -------------------------------------------------------------------------------- /01-postgis/src/test/java/com/github/lonelyleaf/gis/GisDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class GisDemoApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /02-jts/README.md: -------------------------------------------------------------------------------- 1 | # 使用JTS与postgis进行空间数据交互 2 | 3 | 本项目基本实现了JTS与postgis的数据交互,同时实现了基本的postgis的数据 4 | 分析功能。 5 | -------------------------------------------------------------------------------- /02-jts/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'org.springframework.boot' 3 | apply plugin: 'io.spring.dependency-management' 4 | apply plugin: "io.freefair.lombok" 5 | 6 | group = 'xyz.lonelyleaf' 7 | version = '0.0.1-SNAPSHOT' 8 | sourceCompatibility = '11' 9 | 10 | configurations { 11 | compileOnly { 12 | extendsFrom annotationProcessor 13 | } 14 | } 15 | 16 | //添加mapstuct的一些配置 17 | compileJava { 18 | options.compilerArgs = [ 19 | '-Amapstruct.defaultComponentModel=spring',//默认生成为spring bean 20 | '-Amapstruct.unmappedTargetPolicy=ERROR'//未映射字段默认报错 21 | ] 22 | } 23 | 24 | dependencies { 25 | implementation "org.springframework.boot:spring-boot-starter" 26 | implementation 'org.springframework.boot:spring-boot-starter-web' 27 | implementation "org.springframework.boot:spring-boot-starter-actuator" 28 | 29 | //mybatis 30 | implementation "com.baomidou:mybatis-plus-boot-starter:$mybatisPlusVersion" 31 | //数据库管理 32 | implementation 'org.flywaydb:flyway-core' 33 | //pg驱动 34 | implementation "org.postgresql:postgresql:$postgresJdbcVersion" 35 | //postgis 36 | implementation "net.postgis:postgis-jdbc:$postgisJdbcVersion" 37 | 38 | //jackson相关 39 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-json-org" 40 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" 41 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" 42 | //implementation 'com.fasterxml.jackson.datatype:jackson-datatype-joda' 43 | implementation "com.fasterxml.jackson.core:jackson-annotations" 44 | implementation "com.fasterxml.jackson.core:jackson-databind" 45 | implementation "com.fasterxml.jackson.module:jackson-module-parameter-names" 46 | implementation "com.fasterxml.jackson.module:jackson-module-afterburner" 47 | 48 | //java bean转换 49 | implementation "org.mapstruct:mapstruct-jdk8:$mapstructVersion" 50 | annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion" 51 | 52 | //swagger文档 53 | implementation("io.springfox:springfox-swagger2:$springFoxVersion") { 54 | exclude group: 'io.swagger', module: 'swagger-annotations' 55 | exclude group: 'io.swagger', module: 'swagger-models' 56 | } 57 | implementation "io.springfox:springfox-swagger-ui:$springFoxVersion" 58 | //添加下面依赖是为了排除获取api文档时的报错:java.lang.NumberFormatException: For input string: "" 59 | //后面升级springfox后,可以移除下面的两个依赖 60 | implementation 'io.swagger:swagger-annotations:1.5.21' 61 | implementation 'io.swagger:swagger-models:1.5.21' 62 | 63 | //jts 64 | implementation "org.locationtech.jts:jts-core:$jtsVersion" 65 | implementation "org.locationtech.spatial4j:spatial4j:$spatial4jVersion" 66 | //序列化为geojson 67 | //这个没维护了,只有自己改下 68 | //implementation 'com.bedatadriven:jackson-datatype-jts:2.2' 69 | implementation project(':00-jackson-datatype-jts') 70 | //postgis-jdbc-jts 71 | implementation project(':00-postgis-jdbc-jts') 72 | 73 | testImplementation('org.springframework.boot:spring-boot-starter-test') 74 | } 75 | 76 | test { 77 | useJUnitPlatform() 78 | } 79 | -------------------------------------------------------------------------------- /02-jts/img/bbox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/bbox.png -------------------------------------------------------------------------------- /02-jts/img/bind-roads/01-bind-roads.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/bind-roads/01-bind-roads.png -------------------------------------------------------------------------------- /02-jts/img/bind-roads/02-bind-roads.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/bind-roads/02-bind-roads.png -------------------------------------------------------------------------------- /02-jts/img/bind-roads/open-street-map-layers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/bind-roads/open-street-map-layers.png -------------------------------------------------------------------------------- /02-jts/img/geometries/lines.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/geometries/lines.png -------------------------------------------------------------------------------- /02-jts/img/geometries/points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/geometries/points.png -------------------------------------------------------------------------------- /02-jts/img/geometries/polygons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/geometries/polygons.png -------------------------------------------------------------------------------- /02-jts/img/jts-data-output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/jts-data-output.png -------------------------------------------------------------------------------- /02-jts/img/jts-geometry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/jts-geometry.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-geojson/00-add-layer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-geojson/00-add-layer.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-geojson/01-add-layer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-geojson/01-add-layer.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-geojson/02-output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-geojson/02-output.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-geojson/03-outputs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-geojson/03-outputs.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-sql-query/01-qgis-import-sql-query.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-sql-query/01-qgis-import-sql-query.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-sql-query/02-qgis-import-sql-query.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-sql-query/02-qgis-import-sql-query.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-sql-query/03-qgis-import-sql-query.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-sql-query/03-qgis-import-sql-query.png -------------------------------------------------------------------------------- /02-jts/img/qgis-import-sql-query/04-qgis-import-sql-query.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/qgis-import-sql-query/04-qgis-import-sql-query.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_crosses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_crosses.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_disjoint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_disjoint.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_dwithin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_dwithin.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_equals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_equals.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_intersects.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_intersects.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_overlaps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_overlaps.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_touches.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_touches.png -------------------------------------------------------------------------------- /02-jts/img/relations/st_within.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/relations/st_within.png -------------------------------------------------------------------------------- /02-jts/img/rtree-hierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/rtree-hierarchy.png -------------------------------------------------------------------------------- /02-jts/img/sql/district-explain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/sql/district-explain.png -------------------------------------------------------------------------------- /02-jts/img/sql/sql-district-output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/02-jts/img/sql/sql-district-output.png -------------------------------------------------------------------------------- /02-jts/sql/绑路.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:032e7ec4d4834f24a906703592779045cefba513e50850df51d341c80e471de9 3 | size 1249 4 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/JtsGisDemoApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | @SpringBootApplication 23 | public class JtsGisDemoApplication { 24 | 25 | public static void main(String[] args) { 26 | SpringApplication.run(JtsGisDemoApplication.class, args); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/config/JacksonConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.JtsModule; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 6 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 7 | import com.fasterxml.jackson.module.afterburner.AfterburnerModule; 8 | import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; 9 | import org.locationtech.spatial4j.io.jackson.ShapesAsGeoJSONModule; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import com.github.lonelyleaf.gis.util.JsonUtil; 13 | 14 | /** 15 | * jackson配置 16 | */ 17 | @Configuration 18 | public class JacksonConfig { 19 | 20 | /* 21 | * Jackson Afterburner module to speed up serialization/deserialization. 22 | */ 23 | // @Bean 24 | // public AfterburnerModule afterburnerModule() { 25 | // AfterburnerModule module = new AfterburnerModule(); 26 | // // make Afterburner generate bytecode only for public getters/setter and fields 27 | // // without this, Java 9+ complains of "Illegal reflective access" 28 | // module.setUseValueClassLoader(false); 29 | // return module; 30 | // } 31 | 32 | @Bean 33 | public ParameterNamesModule parameterNamesModule() { 34 | return new ParameterNamesModule(); 35 | } 36 | 37 | @Bean 38 | public Jdk8Module jdk8Module() { 39 | return new Jdk8Module(); 40 | } 41 | 42 | @Bean 43 | public JavaTimeModule javaTimeModule() { 44 | return new JavaTimeModule(); 45 | } 46 | 47 | // @Bean 48 | // public ShapesAsGeoJSONModule shapesAsGeoJSONModule(){ 49 | // return new ShapesAsGeoJSONModule(); 50 | // } 51 | 52 | @Bean 53 | public JtsModule jtsModule(){ 54 | return new JtsModule(); 55 | } 56 | 57 | @Bean 58 | public JsonUtil jsonUtil(ObjectMapper objectMapper) { 59 | //只是把objectmapper注入 60 | JsonUtil.objectMapper = objectMapper; 61 | return new JsonUtil(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/config/MybatisPlusConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import com.baomidou.mybatisplus.extension.incrementer.PostgreKeyGenerator; 4 | import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @Configuration 10 | @MapperScan("com.github.lonelyleaf.gis.repo") 11 | public class MybatisPlusConfig { 12 | 13 | @Bean 14 | public PostgreKeyGenerator postgreKeyGenerator() { 15 | return new PostgreKeyGenerator(); 16 | } 17 | 18 | /** 19 | * 分页插件 20 | */ 21 | @Bean 22 | public PaginationInterceptor paginationInterceptor() { 23 | PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); 24 | paginationInterceptor.setLimit(-1); 25 | return paginationInterceptor; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.http.ResponseEntity; 9 | import springfox.documentation.builders.ApiInfoBuilder; 10 | import springfox.documentation.builders.PathSelectors; 11 | import springfox.documentation.builders.RequestHandlerSelectors; 12 | import springfox.documentation.service.ApiInfo; 13 | import springfox.documentation.spi.DocumentationType; 14 | import springfox.documentation.spring.web.plugins.Docket; 15 | import springfox.documentation.swagger.web.DocExpansion; 16 | import springfox.documentation.swagger.web.UiConfiguration; 17 | import springfox.documentation.swagger.web.UiConfigurationBuilder; 18 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 19 | 20 | /** 21 | * swagger配置,这里添加了基于jwt认证的代码 22 | */ 23 | @Configuration 24 | @EnableSwagger2 25 | @EnableConfigurationProperties(SwaggerProperties.class) 26 | @ConditionalOnProperty(value = "swagger.enable", matchIfMissing = true) 27 | public class SwaggerConfig { 28 | 29 | private final SwaggerProperties p; 30 | 31 | public SwaggerConfig(SwaggerProperties p) { 32 | this.p = p; 33 | } 34 | 35 | @Bean 36 | @ConditionalOnMissingBean(Docket.class) 37 | public Docket api() { 38 | Docket docket = new Docket(DocumentationType.SWAGGER_2); 39 | docket.select() 40 | .build() 41 | .pathMapping("/") 42 | .genericModelSubstitutes(ResponseEntity.class) 43 | .enableUrlTemplating(false); 44 | //@formatter:off 45 | return docket 46 | .apiInfo(apiInfo()) 47 | .useDefaultResponseMessages(false) 48 | .select() 49 | .paths(PathSelectors.any()) 50 | //基于包名显示文档 51 | .apis(RequestHandlerSelectors.basePackage(p.getBasePackage())) 52 | .build(); 53 | //@formatter:on 54 | } 55 | 56 | private ApiInfo apiInfo() { 57 | return new ApiInfoBuilder() 58 | .title("seed-demo api文档") 59 | .description("api文档描述,todo") 60 | .build(); 61 | } 62 | 63 | @Bean 64 | public UiConfiguration uiConfig() { 65 | return UiConfigurationBuilder.builder() 66 | .docExpansion(DocExpansion.NONE) 67 | .build(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/config/SwaggerProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * swagger配置参数 7 | */ 8 | @ConfigurationProperties(prefix = "swagger") 9 | public class SwaggerProperties { 10 | private Boolean enable = true; 11 | private String baseUrl = "/"; 12 | private String basePackage = "com.github.lonelyleaf.gis.rest"; 13 | 14 | public Boolean getEnable() { 15 | return enable; 16 | } 17 | 18 | public void setEnable(Boolean enable) { 19 | this.enable = enable; 20 | } 21 | 22 | public String getBaseUrl() { 23 | return baseUrl; 24 | } 25 | 26 | public void setBaseUrl(String baseUrl) { 27 | this.baseUrl = baseUrl; 28 | } 29 | 30 | public String getBasePackage() { 31 | return basePackage; 32 | } 33 | 34 | public void setBasePackage(String basePackage) { 35 | this.basePackage = basePackage; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/dto/GpsDto.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Data; 6 | 7 | import java.util.Date; 8 | 9 | @Data 10 | public class GpsDto { 11 | @JsonFormat(shape = JsonFormat.Shape.STRING, 12 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 13 | timezone = "+08") 14 | @ApiModelProperty("时间") 15 | private Date time; 16 | @ApiModelProperty("设备id") 17 | private String devId; 18 | @ApiModelProperty("位置") 19 | private SimplePoint location; 20 | @ApiModelProperty("卫星定位数") 21 | private int gpsNum; 22 | @ApiModelProperty("GPS定位信息") 23 | private String gpsType; 24 | @ApiModelProperty("对地真北航向角") 25 | private double azimuth; 26 | @ApiModelProperty("地面速率") 27 | private double gndRate; 28 | } 29 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/dto/GpsLine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.dto; 18 | 19 | import com.fasterxml.jackson.annotation.JsonFormat; 20 | import io.swagger.annotations.ApiModelProperty; 21 | import lombok.Data; 22 | import org.locationtech.jts.geom.LineString; 23 | import org.locationtech.jts.geom.MultiPoint; 24 | 25 | import java.util.Date; 26 | 27 | @Data 28 | public class GpsLine { 29 | 30 | @JsonFormat(shape = JsonFormat.Shape.STRING, 31 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 32 | timezone = "+08") 33 | @ApiModelProperty("时间") 34 | private Date start; 35 | @JsonFormat(shape = JsonFormat.Shape.STRING, 36 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 37 | timezone = "+08") 38 | @ApiModelProperty("时间") 39 | private Date end; 40 | @ApiModelProperty("设备id") 41 | private String devId; 42 | 43 | private LineString line; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/dto/SimplePoint.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import org.locationtech.jts.geom.Point; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | @NoArgsConstructor 11 | public class SimplePoint { 12 | 13 | private double x; 14 | private double y; 15 | 16 | public SimplePoint(Point point) { 17 | this.x = point.getX(); 18 | this.y = point.getY(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/entity/DistrictEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.entity; 18 | 19 | 20 | import com.baomidou.mybatisplus.annotation.IdType; 21 | import com.baomidou.mybatisplus.annotation.TableId; 22 | import com.baomidou.mybatisplus.annotation.TableName; 23 | import io.swagger.annotations.ApiModel; 24 | import io.swagger.annotations.ApiModelProperty; 25 | import lombok.AllArgsConstructor; 26 | import lombok.Data; 27 | import lombok.NoArgsConstructor; 28 | import org.locationtech.jts.geom.MultiPolygon; 29 | 30 | @Data 31 | @ApiModel("行政区,这里只有贵州的") 32 | @TableName("t_guizhou_boundary") 33 | @NoArgsConstructor 34 | @AllArgsConstructor 35 | public class DistrictEntity { 36 | 37 | @TableId(type = IdType.AUTO) 38 | private int gid; 39 | @ApiModelProperty("行政区名") 40 | private String name; 41 | @ApiModelProperty("行政区等级,省、市、区县、乡镇") 42 | private String layer; 43 | @ApiModelProperty("编码") 44 | private String code; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/entity/DistrictGeomEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.entity; 18 | 19 | 20 | import com.baomidou.mybatisplus.annotation.TableName; 21 | import io.swagger.annotations.ApiModel; 22 | import lombok.Data; 23 | import lombok.EqualsAndHashCode; 24 | import org.locationtech.jts.geom.MultiPolygon; 25 | 26 | @Data 27 | @EqualsAndHashCode(callSuper=true) 28 | @ApiModel("有行政边界的行政区") 29 | @TableName("t_guizhou_boundary") 30 | public class DistrictGeomEntity extends DistrictEntity { 31 | 32 | private MultiPolygon geom; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/entity/GpsEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.entity; 2 | 3 | import com.baomidou.mybatisplus.annotation.TableName; 4 | import com.fasterxml.jackson.annotation.JsonFormat; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Data; 7 | import org.locationtech.jts.geom.Point; 8 | 9 | import java.util.Date; 10 | 11 | @Data 12 | @TableName("t_gps") 13 | public class GpsEntity { 14 | @JsonFormat(shape = JsonFormat.Shape.STRING, 15 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 16 | timezone = "+08") 17 | @ApiModelProperty("时间") 18 | private Date time; 19 | @ApiModelProperty("设备id") 20 | private String devId; 21 | @ApiModelProperty("位置") 22 | private Point location; 23 | @ApiModelProperty("卫星定位数") 24 | private int gpsNum; 25 | @ApiModelProperty("GPS定位信息") 26 | private String gpsType; 27 | @ApiModelProperty("对地真北航向角") 28 | private double azimuth; 29 | @ApiModelProperty("地面速率") 30 | private double gndRate; 31 | } 32 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mapper/GpsMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.mapper; 2 | 3 | 4 | import com.github.lonelyleaf.gis.dto.GpsDto; 5 | import com.github.lonelyleaf.gis.dto.SimplePoint; 6 | import com.github.lonelyleaf.gis.entity.GpsEntity; 7 | import com.github.lonelyleaf.gis.util.JtsUtil; 8 | import org.locationtech.jts.geom.*; 9 | 10 | @org.mapstruct.Mapper 11 | public interface GpsMapper { 12 | 13 | GpsDto toDto(GpsEntity entity); 14 | 15 | GpsEntity toEntity(GpsDto entity); 16 | 17 | default SimplePoint toSimple(Point point) { 18 | return new SimplePoint(point); 19 | } 20 | 21 | default Point toGisPoint(SimplePoint point) { 22 | Coordinate coordinate = new CoordinateXY(point.getX(), point.getY()); 23 | return JtsUtil.geometryFactory4326.createPoint(coordinate); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/AbstractJtsGeometryTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.postgis.jts.JtsGeometry; 20 | import org.apache.ibatis.type.BaseTypeHandler; 21 | import org.apache.ibatis.type.JdbcType; 22 | import org.locationtech.jts.geom.Geometry; 23 | 24 | import java.sql.CallableStatement; 25 | import java.sql.PreparedStatement; 26 | import java.sql.ResultSet; 27 | import java.sql.SQLException; 28 | 29 | @SuppressWarnings("unchecked") 30 | public abstract class AbstractJtsGeometryTypeHandler extends BaseTypeHandler { 31 | 32 | public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { 33 | ps.setObject(i, new JtsGeometry(parameter)); 34 | } 35 | 36 | public T getNullableResult(ResultSet rs, String columnName) throws SQLException { 37 | JtsGeometry jtsGeometry = (JtsGeometry) rs.getObject(columnName); 38 | if (jtsGeometry == null) { 39 | return null; 40 | } 41 | return (T) jtsGeometry.getGeometry(); 42 | } 43 | 44 | public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 45 | JtsGeometry jtsGeometry = (JtsGeometry) rs.getObject(columnIndex); 46 | if (jtsGeometry == null) { 47 | return null; 48 | } 49 | return (T) jtsGeometry.getGeometry(); 50 | } 51 | 52 | public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 53 | JtsGeometry jtsGeometry = (JtsGeometry) cs.getObject(columnIndex); 54 | if (jtsGeometry == null) { 55 | return null; 56 | } 57 | return (T) jtsGeometry.getGeometry(); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsLineStringTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.locationtech.jts.geom.LineString; 21 | 22 | @MappedTypes(LineString.class) 23 | public class JtsLineStringTypeHandler extends AbstractJtsGeometryTypeHandler { 24 | } 25 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsLinearRingTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.locationtech.jts.geom.LinearRing; 21 | import org.locationtech.jts.geom.MultiLineString; 22 | 23 | @MappedTypes(LinearRing.class) 24 | public class JtsLinearRingTypeHandler extends AbstractJtsGeometryTypeHandler { 25 | } 26 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsMultiLineStringTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.locationtech.jts.geom.MultiLineString; 21 | import org.locationtech.jts.geom.MultiPolygon; 22 | 23 | @MappedTypes(MultiLineString.class) 24 | public class JtsMultiLineStringTypeHandler extends AbstractJtsGeometryTypeHandler { 25 | } 26 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsMultiPointTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.lonelyleaf.gis.mybatis.jts; 17 | 18 | import org.apache.ibatis.type.MappedTypes; 19 | import org.locationtech.jts.geom.MultiPoint; 20 | 21 | @MappedTypes(MultiPoint.class) 22 | public class JtsMultiPointTypeHandler extends AbstractJtsGeometryTypeHandler { 23 | } 24 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsMultiPolygonTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.locationtech.jts.geom.MultiPolygon; 21 | import org.locationtech.jts.geom.Polygon; 22 | 23 | @MappedTypes(MultiPolygon.class) 24 | public class JtsMultiPolygonTypeHandler extends AbstractJtsGeometryTypeHandler { 25 | } 26 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsPointTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.locationtech.jts.geom.Point; 21 | 22 | @MappedTypes(Point.class) 23 | public class JtsPointTypeHandler extends AbstractJtsGeometryTypeHandler { 24 | } 25 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/jts/JtsPolygonTypeHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.jts; 18 | 19 | import org.apache.ibatis.type.MappedTypes; 20 | import org.locationtech.jts.geom.Polygon; 21 | 22 | @MappedTypes(Polygon.class) 23 | public class JtsPolygonTypeHandler extends AbstractJtsGeometryTypeHandler { 24 | } 25 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/mybatis/wrapper/PostgisWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.mybatis.wrapper; 18 | 19 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 20 | import org.locationtech.jts.geom.Geometry; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * 实现了部分的gis函数作为查询条件,详细内容请参考 27 | * 28 | *

https://postgis.net/docs/manual-dev/reference.html 29 | */ 30 | public class PostgisWrapper extends QueryWrapper { 31 | 32 | /** 33 | * 返回a是否完全在b图形中 34 | * 35 | * @param a 图形a,{@link Geometry}或表字段名 36 | * @param b 图形b,{@link Geometry}或表字段名 37 | * @see ST_Within 38 | */ 39 | public QueryWrapper stWithIn(Object a, Object b) { 40 | return gisFunction("ST_Within(%s,%s)", a, b); 41 | } 42 | 43 | /** 44 | * 返回a是否在b图形的指定范围中,要精确计算地理位置坐标关系,使用{@link #stDWithInGeography} 45 | * 46 | * @param a 图形a,{@link Geometry}或表字段名 47 | * @param b 图形b,{@link Geometry}或表字段名 48 | * @param distance 距离单位取决于srid 49 | * @see ST_DWithin 50 | */ 51 | public QueryWrapper stDWithIn(Object a, Object b, double distance) { 52 | return gisFunction("ST_DWithin(%s,%s,%s)", a, b, distance); 53 | } 54 | 55 | /** 56 | * 返回a是否在b图形的指定范围中,使用的是地理坐标进行计算。 57 | * 58 | * @param a 图形a,{@link Geometry}或表字段名 59 | * @param b 图形b,{@link Geometry}或表字段名 60 | * @param distance 距离单位取决于图像的srid 61 | * @see ST_DWithin 62 | */ 63 | public QueryWrapper stDWithInGeography(Object a, Object b, double distance) { 64 | return gisFunction("ST_DWithin(%s::geography,%s::geography,%s,true)", a, b, distance); 65 | } 66 | 67 | /** 68 | * 拼接postgis function的sql,string类型参数会当作表字段 69 | * 70 | * @param functionSql sql模板,使用的{@link String#format} 71 | * @param params {@link Geometry}或{@link String}.string类型参数会当作表字段 72 | */ 73 | private QueryWrapper gisFunction(String functionSql, Object... params) { 74 | Object[] paramHolders = new Object[params.length]; 75 | List paramsList = new ArrayList<>(params.length); 76 | for (int i = 0; i < params.length; i++) { 77 | Object param = params[i]; 78 | if (param instanceof String) { 79 | //字符串都当作表的字段 80 | //可能被注入,暂时没其它好办法 81 | paramHolders[i] = param; 82 | } else { 83 | paramHolders[i] = "{" + paramsList.size() + "}"; 84 | paramsList.add(param); 85 | } 86 | } 87 | String sql = String.format(functionSql, paramHolders); 88 | return apply(sql, paramsList.toArray()); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/repo/DistrictGeomRepo.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.repo; 2 | 3 | 4 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 5 | import com.github.lonelyleaf.gis.entity.DistrictEntity; 6 | import com.github.lonelyleaf.gis.entity.DistrictGeomEntity; 7 | 8 | /** 9 | * 行政区仓储 10 | */ 11 | public interface DistrictGeomRepo extends BaseMapper { 12 | } 13 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/repo/DistrictRepo.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.repo; 2 | 3 | 4 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 5 | import com.github.lonelyleaf.gis.entity.DistrictEntity; 6 | import com.github.lonelyleaf.gis.entity.DistrictGeomEntity; 7 | import org.apache.ibatis.annotations.Param; 8 | import org.locationtech.jts.geom.Geometry; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * 行政区仓储 14 | */ 15 | public interface DistrictRepo extends BaseMapper { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/repo/GisRepo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2020] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.repo; 18 | 19 | import org.apache.ibatis.annotations.Param; 20 | import org.apache.ibatis.annotations.Select; 21 | import org.locationtech.jts.geom.Geometry; 22 | import org.locationtech.jts.geom.LineString; 23 | import org.locationtech.jts.geom.MultiLineString; 24 | import org.locationtech.jts.geom.Point; 25 | 26 | /** 27 | * 详细内容请参考 28 | *

29 | * https://postgis.net/docs/manual-dev/reference.html 30 | */ 31 | @org.apache.ibatis.annotations.Mapper 32 | public interface GisRepo { 33 | 34 | /** 35 | * @return 返回单位与srid有关,如果是wgs84,那么单位会是度 36 | */ 37 | @Select("select ST_Distance(#{a},#{b})") 38 | double stDistance(@Param("a") Geometry a, @Param("b") Geometry b); 39 | 40 | /** 41 | * @return 返回的单位是米 42 | */ 43 | @Select("select ST_Distance(#{a}::geography,#{b}::geography)") 44 | double stDistanceGeography(@Param("a") Geometry a, @Param("b") Geometry b); 45 | 46 | /** 47 | * https://postgis.net/docs/ST_DistanceSphere.html 48 | */ 49 | @Select("select ST_DistanceSphere(#{a},#{b})") 50 | double stDistanceSphere(@Param("a") Geometry a, @Param("b") Geometry b); 51 | 52 | /** 53 | * https://postgis.net/docs/ST_DistanceSphere.html 54 | */ 55 | @Select("select ST_LineMerge(#{a})") 56 | LineString stLineMerge(@Param("a") MultiLineString a); 57 | 58 | @Select("with measure as (select roads.gid as road_gid,\n" + 59 | " ST_Distance(#{point}, roads.geom) as distance,\n" + 60 | " ST_LineLocatePoint(roads.geom::geometry, #{point}) AS measure\n" + 61 | " from t_zunyi_roads roads\n" + 62 | " where st_dwithin(roads.geom, #{point}, #{roadDistance}, false)\n" + 63 | " order by roads.gid, distance\n" + 64 | " limit 1)\n" + 65 | "select ST_LineInterpolatePoint(roads.geom::geometry, measure.measure)\n" + 66 | "from measure\n" + 67 | " left join t_zunyi_roads roads on measure.road_gid = roads.gid") 68 | Point bindRoad(@Param("point") Point point,@Param("roadDistance") double roadDistance); 69 | 70 | } 71 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/repo/GpsRepo.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.repo; 2 | 3 | 4 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 5 | import com.github.lonelyleaf.gis.entity.GpsEntity; 6 | 7 | /** 8 | * GPS相关 9 | */ 10 | public interface GpsRepo extends BaseMapper { 11 | } 12 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/rest/DistrictController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.rest; 18 | 19 | import com.github.lonelyleaf.gis.entity.DistrictEntity; 20 | import com.github.lonelyleaf.gis.entity.DistrictGeomEntity; 21 | import com.github.lonelyleaf.gis.repo.DistrictGeomRepo; 22 | import com.github.lonelyleaf.gis.service.DistrictService; 23 | import com.github.lonelyleaf.gis.util.JtsUtil; 24 | import com.google.common.base.Strings; 25 | import io.swagger.annotations.Api; 26 | import io.swagger.annotations.ApiParam; 27 | import io.swagger.annotations.ApiResponse; 28 | import io.swagger.annotations.ApiResponses; 29 | import org.locationtech.jts.geom.CoordinateXY; 30 | import org.locationtech.jts.geom.Point; 31 | import org.springframework.validation.annotation.Validated; 32 | import org.springframework.web.bind.annotation.*; 33 | 34 | import java.util.Arrays; 35 | import java.util.Collections; 36 | import java.util.List; 37 | 38 | @RestController 39 | @RequestMapping("/district") 40 | @Validated 41 | @Api(description = "行政区与边界") 42 | public class DistrictController { 43 | 44 | private final DistrictService districtService; 45 | 46 | public DistrictController(DistrictService districtService) { 47 | this.districtService = districtService; 48 | } 49 | 50 | @GetMapping 51 | @ApiResponses( 52 | {@ApiResponse(code = 200, message = "geom=false时,不会携带geom字段")} 53 | ) 54 | public List list( 55 | @ApiParam("行政区名,支持like") 56 | @RequestParam(name = "name", required = false) String name, 57 | @ApiParam("层级,省,市,区县。多个用逗号分隔") 58 | @RequestParam(name = "layers", required = false) String layersStr, 59 | @ApiParam("是否携带geom对象,geom数据量很大,一般查询可以不携带") 60 | @RequestParam(name = "geom", required = false, defaultValue = "false") boolean geom) { 61 | List layers = Collections.emptyList(); 62 | if (!Strings.isNullOrEmpty(layersStr)) { 63 | layers = Arrays.asList(layersStr.split(",")); 64 | } 65 | return districtService.findBy(name, layers, geom); 66 | } 67 | 68 | @GetMapping("/within") 69 | public List within( 70 | @ApiParam("点,x和y,逗号分隔") 71 | @RequestParam(name = "point", required = false) String pointStr, 72 | @ApiParam("是否携带geom对象,geom数据量很大,一般查询可以不携带") 73 | @RequestParam(name = "geom", required = false, defaultValue = "false") boolean geom) { 74 | String[] pointArray = pointStr.split(","); 75 | Point point = JtsUtil.newPoint(Double.parseDouble(pointArray[0]), Double.parseDouble(pointArray[1])); 76 | return districtService.withIn(point, geom); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/rest/GpsController.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.rest; 2 | 3 | import com.github.lonelyleaf.gis.dto.GpsDto; 4 | import com.github.lonelyleaf.gis.dto.GpsLine; 5 | import com.github.lonelyleaf.gis.dto.SimplePoint; 6 | import com.github.lonelyleaf.gis.entity.GpsEntity; 7 | import com.github.lonelyleaf.gis.mapper.GpsMapper; 8 | import com.github.lonelyleaf.gis.service.GpsService; 9 | import com.github.lonelyleaf.gis.util.JtsUtil; 10 | import com.google.common.base.Strings; 11 | import io.swagger.annotations.Api; 12 | import org.locationtech.jts.geom.Coordinate; 13 | import org.locationtech.jts.geom.Point; 14 | import org.springframework.format.annotation.DateTimeFormat; 15 | import org.springframework.validation.annotation.Validated; 16 | import org.springframework.web.bind.annotation.*; 17 | 18 | import javax.validation.constraints.NotEmpty; 19 | import java.util.Date; 20 | import java.util.List; 21 | import java.util.stream.Collectors; 22 | 23 | @RestController 24 | @RequestMapping("/gps") 25 | @Validated 26 | @Api(description = "轨迹数据") 27 | public class GpsController { 28 | 29 | private final GpsService gpsService; 30 | private final GpsMapper gpsMapper; 31 | 32 | public GpsController(GpsService gpsService, GpsMapper gpsMapper) { 33 | this.gpsService = gpsService; 34 | this.gpsMapper = gpsMapper; 35 | } 36 | 37 | @GetMapping("/history") 38 | public List history( 39 | @RequestParam(value = "devId", required = false) 40 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 41 | @RequestParam(value = "bTime", required = false) 42 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 43 | @RequestParam(value = "eTime", required = false) 44 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 45 | return gpsService.history(devId, bTime, eTime).stream() 46 | .map(gpsMapper::toDto) 47 | .collect(Collectors.toList()); 48 | } 49 | 50 | @GetMapping("/line") 51 | public GpsLine line( 52 | @NotEmpty 53 | @RequestParam(value = "devId", required = false) 54 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 55 | @RequestParam(value = "bTime", required = false) 56 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 57 | @RequestParam(value = "eTime", required = false) 58 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 59 | 60 | List history = gpsService.history(devId, bTime, eTime); 61 | GpsLine gpsLine = new GpsLine(); 62 | gpsLine.setDevId(devId); 63 | gpsLine.setStart(bTime); 64 | gpsLine.setEnd(eTime); 65 | 66 | Coordinate[] points = history.stream() 67 | .map(entity -> entity.getLocation().getCoordinate()) 68 | .toArray(Coordinate[]::new); 69 | 70 | gpsLine.setLine(JtsUtil.geometryFactory4326.createLineString(points)); 71 | return gpsLine; 72 | } 73 | 74 | @GetMapping("/history/simple") 75 | public List simpleHistory( 76 | @RequestParam(value = "devId", required = false) 77 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 78 | @RequestParam(value = "bTime", required = false) 79 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 80 | @RequestParam(value = "eTime", required = false) 81 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 82 | List history = gpsService.history(devId, bTime, eTime); 83 | return history.stream() 84 | .map(gpsEntity -> new SimplePoint(gpsEntity.getLocation())) 85 | .collect(Collectors.toList()); 86 | } 87 | 88 | @PostMapping 89 | public String simpleHistory(@RequestBody GpsDto gpsDto) { 90 | if (gpsDto.getTime() == null) { 91 | gpsDto.setTime(new Date()); 92 | } 93 | if (Strings.isNullOrEmpty(gpsDto.getDevId())) { 94 | throw new IllegalArgumentException("devId不能为空"); 95 | } 96 | 97 | GpsEntity entity = gpsMapper.toEntity(gpsDto); 98 | gpsService.save(entity); 99 | return "ok"; 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/service/DistrictService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.service; 18 | 19 | import com.baomidou.mybatisplus.core.conditions.Wrapper; 20 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 21 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 22 | import com.github.lonelyleaf.gis.entity.DistrictEntity; 23 | import com.github.lonelyleaf.gis.entity.DistrictGeomEntity; 24 | import com.github.lonelyleaf.gis.mybatis.wrapper.PostgisWrapper; 25 | import com.github.lonelyleaf.gis.repo.DistrictGeomRepo; 26 | import com.github.lonelyleaf.gis.repo.DistrictRepo; 27 | import com.google.common.base.Strings; 28 | import org.locationtech.jts.geom.Point; 29 | import org.springframework.beans.factory.annotation.Autowired; 30 | import org.springframework.jdbc.core.JdbcTemplate; 31 | import org.springframework.stereotype.Service; 32 | 33 | import java.util.List; 34 | 35 | @Service 36 | public class DistrictService extends ServiceImpl { 37 | 38 | @Autowired 39 | DistrictGeomRepo districtGeomRepo; 40 | 41 | @SuppressWarnings("unchecked") 42 | public List findBy(String name, List layers, boolean geom) { 43 | QueryWrapper wrapper = new QueryWrapper<>(); 44 | if (!Strings.isNullOrEmpty(name)) { 45 | wrapper.like("name", name); 46 | } 47 | if (layers != null && layers.size() != 0) { 48 | wrapper.in("layer", layers); 49 | } 50 | 51 | if (geom) { 52 | return districtGeomRepo.selectList((Wrapper) wrapper); 53 | } else { 54 | return list((Wrapper) wrapper); 55 | } 56 | } 57 | 58 | public List withIn(Point point, boolean geom) { 59 | PostgisWrapper wrapper = new PostgisWrapper<>(); 60 | wrapper.stDWithIn(point, "geom", 5); 61 | 62 | if (geom) { 63 | return districtGeomRepo.selectList((Wrapper) wrapper); 64 | } else { 65 | return list((Wrapper) wrapper); 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/service/GpsService.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.service; 2 | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 4 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 5 | import com.github.lonelyleaf.gis.entity.GpsEntity; 6 | import com.github.lonelyleaf.gis.util.JtsUtil; 7 | import com.google.common.base.Strings; 8 | import org.locationtech.jts.geom.MultiPoint; 9 | import org.springframework.stereotype.Service; 10 | import com.github.lonelyleaf.gis.repo.GpsRepo; 11 | 12 | import java.util.Date; 13 | import java.util.List; 14 | 15 | @Service 16 | public class GpsService extends ServiceImpl { 17 | 18 | public List history(String devId, Date bTime, Date eTime) { 19 | QueryWrapper wrapper = new QueryWrapper<>(); 20 | wrapper.orderByAsc("time"); 21 | if (!Strings.isNullOrEmpty(devId)) { 22 | wrapper.eq("dev_id", devId); 23 | } 24 | if (bTime != null) { 25 | wrapper.ge("time", bTime); 26 | } 27 | if (eTime != null) { 28 | wrapper.le("time", eTime); 29 | } 30 | return list(wrapper); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/util/JsonUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.util; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | 6 | import java.io.IOException; 7 | 8 | /** 9 | * 方便序列化json 10 | */ 11 | public class JsonUtil { 12 | 13 | public static ObjectMapper objectMapper = new ObjectMapper() 14 | .registerModules(ObjectMapper.findModules()); 15 | 16 | public static String toJson(Object obj) { 17 | try { 18 | return objectMapper.writeValueAsString(obj); 19 | } catch (JsonProcessingException e) { 20 | throw new RuntimeException(e); 21 | } 22 | } 23 | 24 | public static String toPrettyJson(Object obj) { 25 | try { 26 | return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); 27 | } catch (JsonProcessingException e) { 28 | throw new RuntimeException(e); 29 | } 30 | } 31 | 32 | public static T fromJson(String s, Class clazz) { 33 | try { 34 | return objectMapper.readValue(s, clazz); 35 | } catch (IOException e) { 36 | throw new RuntimeException(e); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /02-jts/src/main/java/com/github/lonelyleaf/gis/util/JtsUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.util; 18 | 19 | 20 | import org.locationtech.jts.geom.CoordinateXY; 21 | import org.locationtech.jts.geom.GeometryFactory; 22 | import org.locationtech.jts.geom.Point; 23 | import org.locationtech.jts.geom.PrecisionModel; 24 | 25 | public class JtsUtil { 26 | 27 | /** 28 | * srid为4326 29 | */ 30 | public static final GeometryFactory geometryFactory4326 = new GeometryFactory(new PrecisionModel(), 4326); 31 | 32 | public static Point newPoint(double x, double y) { 33 | return geometryFactory4326.createPoint(new CoordinateXY(x, y)); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | jackson: 5 | serialization: 6 | write_dates_as_timestamps: true 7 | 8 | servlet: 9 | multipart: 10 | max-file-size: 10MB 11 | 12 | jdbc: 13 | template: 14 | query-timeout: 15 15 | 16 | flyway: 17 | enabled: true 18 | 19 | datasource: 20 | # driver-class-name: org.postgresql.Driver 21 | # url: jdbc:postgresql://localhost:5432/gis-test 22 | #这里使用的驱动不一样!,可以自动添加jts的各种类型 23 | driver-class-name: org.postgis.jts.JtsWrapper 24 | #url也不一样 25 | url: jdbc:postgres_jts://localhost:5432/jts-test 26 | username: postgres 27 | password: admin 28 | initialization-mode: never 29 | 30 | mybatis-plus: 31 | # configuration: 32 | # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 33 | type-handlers-package: "com.github.lonelyleaf.gis.mybatis.jts" -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.0__init.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:796897dec9d7113301834ca412d852f3c553ba26a47eff5e584afc628eae32ea 3 | size 663 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.1__gps_sample_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:c1af1aed4a980e272ca3eea7adaf8970dd2e9d38b9de91b90a1854207cdbc726 3 | size 1202496 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.2__index_gps.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:236c5272d6bfffc4f572158e160823f87dfb09108a3779429dc96bd02ddda471 3 | size 125 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.3__guizhou_boundary.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:6e82eb84e3d82738a32eb3d8320e6e4e083d60d13e43e1ee6bce24979dc0c238 3 | size 948 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.4__guizhou_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:33923dc849844cd9f2fed4aa8d453bf396d9b534a6878669cd901945cdcb306a 3 | size 5853288 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.5__guizhou_index.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f101e0687d5c1d6da7297668b1cb9c66af99e0f74844c272fe8f91476b9716ec 3 | size 773 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.6__zunyi_roads.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:facabc9a1f2a723be09d795b90f6a2ee515ff77633568424d730ebe8fbef3613 3 | size 912 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.7__zunyi_roads_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:579877976bbcdf09bcaa3f78310b8a2f29b7ccb1a6e964b84d69e71c5ba08646 3 | size 8081799 4 | -------------------------------------------------------------------------------- /02-jts/src/main/resources/db/migration/V1.8__zunyi_roads_index.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:240360354d3869ae4cd7624c45ce30db3a1af1da8b12ce0b56bc331baf5b3820 3 | size 764 4 | -------------------------------------------------------------------------------- /02-jts/src/test/java/com/github/lonelyleaf/gis/PostgisJtsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2020] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis; 18 | 19 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 20 | import com.github.lonelyleaf.gis.entity.GpsEntity; 21 | import com.github.lonelyleaf.gis.repo.GpsRepo; 22 | import com.google.common.base.Strings; 23 | import lombok.extern.slf4j.Slf4j; 24 | import org.junit.Test; 25 | import org.junit.runner.RunWith; 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.boot.test.context.SpringBootTest; 28 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 29 | 30 | import java.util.List; 31 | 32 | @RunWith(SpringJUnit4ClassRunner.class) 33 | @SpringBootTest(classes = JtsGisDemoApplication.class) 34 | @Slf4j 35 | public class PostgisJtsTest { 36 | 37 | @Autowired 38 | GpsRepo gpsRepo; 39 | 40 | @Test 41 | public void testReadData() { 42 | QueryWrapper wrapper = new QueryWrapper<>(); 43 | wrapper.orderByAsc("time"); 44 | wrapper.eq("dev_id", "0004r"); 45 | wrapper.last("limit 100"); 46 | List list = gpsRepo.selectList(wrapper); 47 | for (GpsEntity entity : list) { 48 | System.out.println(entity); 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /02-jts/src/test/java/com/github/lonelyleaf/gis/PostgisWrapperTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2020] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis; 18 | 19 | import com.github.lonelyleaf.gis.entity.DistrictEntity; 20 | import com.github.lonelyleaf.gis.entity.DistrictGeomEntity; 21 | import com.github.lonelyleaf.gis.mybatis.wrapper.PostgisWrapper; 22 | import com.github.lonelyleaf.gis.repo.DistrictGeomRepo; 23 | import com.github.lonelyleaf.gis.repo.DistrictRepo; 24 | import com.github.lonelyleaf.gis.repo.GisRepo; 25 | import com.github.lonelyleaf.gis.util.JtsUtil; 26 | import lombok.extern.slf4j.Slf4j; 27 | import org.junit.Assert; 28 | import org.junit.jupiter.api.Test; 29 | import org.junit.runner.RunWith; 30 | import org.locationtech.jts.geom.Point; 31 | import org.springframework.beans.factory.annotation.Autowired; 32 | import org.springframework.boot.test.context.SpringBootTest; 33 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 34 | 35 | import java.util.List; 36 | 37 | @RunWith(SpringJUnit4ClassRunner.class) 38 | @SpringBootTest(classes = JtsGisDemoApplication.class) 39 | @Slf4j 40 | public class PostgisWrapperTest { 41 | 42 | @Autowired 43 | DistrictRepo districtRepo; 44 | @Autowired 45 | DistrictGeomRepo districtGeomRepo; 46 | @Autowired 47 | GisRepo gisRepo; 48 | 49 | private DistrictEntity guiyangDistrict = new DistrictEntity(2, "贵阳市", "市", "112520100000000"); 50 | private DistrictEntity zunyiDistrict = new DistrictEntity(4, "遵义市", "市", "112520300000000"); 51 | 52 | /** 53 | * 查找该坐标在哪个市 54 | */ 55 | @Test 56 | public void testStWithIn() { 57 | Point point = JtsUtil.newPoint(106.677, 26.572); 58 | PostgisWrapper wrapper = new PostgisWrapper<>(); 59 | wrapper.eq("layer", "市"); 60 | wrapper.stWithIn(point, "geom"); 61 | 62 | List list = districtRepo.selectList(wrapper); 63 | for (DistrictEntity entity : list) { 64 | log.info(entity.toString()); 65 | } 66 | Assert.assertEquals(1, list.size()); 67 | Assert.assertEquals(guiyangDistrict, list.get(0)); 68 | } 69 | 70 | /** 71 | * 查找贵阳市内的行政区 72 | */ 73 | @Test 74 | public void testStWithIn2() { 75 | PostgisWrapper wrapper = new PostgisWrapper<>(); 76 | wrapper.eq("layer", "市"); 77 | wrapper.eq("name", "贵阳市"); 78 | 79 | List geomEntities = districtGeomRepo.selectList(wrapper); 80 | Assert.assertEquals(1, geomEntities.size()); 81 | DistrictGeomEntity guiyang = geomEntities.get(0); 82 | Assert.assertEquals("贵阳市", guiyang.getName()); 83 | 84 | 85 | PostgisWrapper wrapper1 = new PostgisWrapper<>(); 86 | wrapper1.stWithIn("geom", guiyang.getGeom()); 87 | List list = districtRepo.selectList(wrapper1); 88 | for (DistrictEntity districtEntity : list) { 89 | log.info(districtEntity.toString()); 90 | } 91 | String[] results = new String[]{"贵阳市", "南明区", "云岩区", "花溪区", "乌当区", 92 | "白云区", "观山湖区", "息烽县", "开阳县", "修文县", "清镇市"}; 93 | 94 | for (String result : results) { 95 | DistrictEntity entity = list.stream().filter(input -> input.getName().equals(result)).findFirst().get(); 96 | Assert.assertNotNull("数据不全,未包含 " + result, entity); 97 | } 98 | 99 | } 100 | 101 | @Test 102 | public void testStDistance() { 103 | Point a = JtsUtil.newPoint(106.677, 26.572); 104 | Point c = JtsUtil.newPoint(106.433, 29.524); 105 | //这里返回的距离单位是度,并且并未计算两点的球面距离 106 | double distance1 = gisRepo.stDistance(a, c); 107 | Assert.assertEquals(2.9620668459709028, distance1, 0.000001); 108 | log.info("距离{}", distance1); 109 | //这里返回的距离是米 110 | double distance2 = gisRepo.stDistanceGeography(a, c); 111 | Assert.assertEquals(328019.76934956, distance2, 0.000000001); 112 | log.info("距离{}", distance2); 113 | //这里返回的距离是米 114 | double distance3 = gisRepo.stDistanceGeography(a, c); 115 | Assert.assertEquals(328019.76934956, distance3, 0.000000001); 116 | log.info("距离{}", distance2); 117 | } 118 | 119 | @Test 120 | public void testStDWithIn() { 121 | //这个点是重庆 122 | Point a = JtsUtil.newPoint(106.433, 29.524); 123 | PostgisWrapper wrapper = new PostgisWrapper<>(); 124 | wrapper.eq("layer", "市"); 125 | // wrapper.stDWithInGeography(a, "geom",328019); 126 | //遵义的北部距重庆不到100km 127 | wrapper.stDWithInGeography(a, "geom", 100000); 128 | 129 | List list = districtRepo.selectList(wrapper); 130 | for (DistrictEntity entity : list) { 131 | log.info(entity.toString()); 132 | } 133 | Assert.assertEquals(1, list.size()); 134 | Assert.assertEquals(zunyiDistrict, list.get(0)); 135 | } 136 | 137 | @Test 138 | public void testBindRoad() { 139 | Point point = JtsUtil.newPoint(106.873251, 27.530133); 140 | Point lineRef = gisRepo.bindRoad(point, 30); 141 | log.info(lineRef.toString()); 142 | } 143 | 144 | } 145 | -------------------------------------------------------------------------------- /02-jts/src/test/java/com/github/lonelyleaf/gis/TestGeoJson.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.JtsModule; 4 | import com.fasterxml.jackson.core.JsonProcessingException; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.junit.jupiter.api.Test; 8 | import org.locationtech.jts.geom.Coordinate; 9 | import org.locationtech.jts.geom.GeometryFactory; 10 | import org.locationtech.jts.geom.Point; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | 13 | @Slf4j 14 | class TestGeoJson { 15 | 16 | @Test 17 | public void test() throws JsonProcessingException { 18 | ObjectMapper mapper = new ObjectMapper(); 19 | mapper.registerModule(new JtsModule()); 20 | 21 | 22 | GeometryFactory gf = new GeometryFactory(); 23 | Point point = gf.createPoint(new Coordinate(1.2345678, 2.3456789)); 24 | String geojson = mapper.writeValueAsString(point); 25 | log.info(geojson); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /03-jpa/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'org.springframework.boot' 3 | apply plugin: 'io.spring.dependency-management' 4 | apply plugin: "io.freefair.lombok" 5 | 6 | group = 'xyz.lonelyleaf' 7 | version = '0.0.1-SNAPSHOT' 8 | sourceCompatibility = '11' 9 | 10 | ext { 11 | querydslVersion = "4.4.0" 12 | } 13 | 14 | //添加mapstuct的一些配置 15 | compileJava { 16 | options.compilerArgs = [ 17 | '-Amapstruct.defaultComponentModel=spring',//默认生成为spring bean 18 | '-Amapstruct.unmappedTargetPolicy=ERROR',//未映射字段默认报错 19 | // '-Aspatial=true'//未映射字段默认报错 20 | ] 21 | } 22 | 23 | dependencies { 24 | implementation "org.springframework.boot:spring-boot-starter" 25 | implementation 'org.springframework.boot:spring-boot-starter-web' 26 | implementation('org.springframework.boot:spring-boot-starter-data-jpa') 27 | implementation "org.springframework.boot:spring-boot-starter-actuator" 28 | implementation 'org.springframework.boot:spring-boot-starter-validation' 29 | 30 | //数据库管理 31 | implementation 'org.flywaydb:flyway-core' 32 | //pg驱动 33 | implementation "org.postgresql:postgresql:$postgresJdbcVersion" 34 | // //postgis 35 | // implementation "net.postgis:postgis-jdbc:$postgisJdbcVersion" 36 | //hibernate-spatial 37 | implementation 'org.hibernate:hibernate-spatial' 38 | //querydsl 39 | implementation "com.querydsl:querydsl-jpa:$querydslVersion" 40 | implementation "com.querydsl:querydsl-spatial:$querydslVersion" 41 | annotationProcessor "com.querydsl:querydsl-spatial:$querydslVersion" 42 | annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa" 43 | 44 | //jdk9后jdk中不再包含javax的包,但querydsl有依赖,这里补全下 45 | annotationProcessor "javax.persistence:javax.persistence-api:2.2" 46 | annotationProcessor 'javax.annotation:javax.annotation-api:1.3.2' 47 | 48 | //jackson相关 49 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-json-org" 50 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-hibernate5" 51 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" 52 | implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" 53 | implementation "com.fasterxml.jackson.core:jackson-annotations" 54 | implementation "com.fasterxml.jackson.core:jackson-databind" 55 | implementation "com.fasterxml.jackson.module:jackson-module-parameter-names" 56 | implementation "com.fasterxml.jackson.module:jackson-module-afterburner" 57 | 58 | implementation 'javax.persistence:javax.persistence-api:2.2' 59 | 60 | //java bean转换 61 | implementation "org.mapstruct:mapstruct:$mapstructVersion" 62 | annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion" 63 | 64 | //swagger文档 65 | implementation "io.springfox:springfox-boot-starter:$springFoxVersion" 66 | implementation "io.springfox:springfox-swagger-ui:$springFoxVersion" 67 | implementation "io.springfox:springfox-bean-validators:$springFoxVersion" 68 | 69 | implementation "org.geolatte:geolatte-geom:$geolatteGeomVersion" 70 | implementation "org.geolatte:geolatte-geojson:$geolatteGeomVersion" 71 | 72 | // //jts 73 | // implementation "org.locationtech.jts:jts-core:$jtsVersion" 74 | // implementation "org.locationtech.spatial4j:spatial4j:$spatial4jVersion" 75 | //序列化为geojson 76 | //这个没维护了,只有自己改下 77 | //implementation 'com.bedatadriven:jackson-datatype-jts:2.2' 78 | implementation project(':00-jackson-datatype-jts') 79 | //postgis-jdbc-jts 80 | implementation project(':00-postgis-jdbc-jts') 81 | 82 | testImplementation('org.springframework.boot:spring-boot-starter-test') 83 | } 84 | 85 | dependencyManagement { 86 | resolutionStrategy { 87 | //spring boot的配置中会有缓存,这样就无法实时更新SNAPSHOT的包,这里关闭缓存 88 | cacheChangingModulesFor 0, 'seconds' 89 | } 90 | imports { 91 | mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") 92 | } 93 | dependencies { 94 | dependency 'com.google.guava:guava:27.0.1-jre' 95 | } 96 | } 97 | 98 | test { 99 | useJUnitPlatform() 100 | } 101 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/JpaGisDemoApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | @SpringBootApplication 23 | public class JpaGisDemoApplication { 24 | 25 | public static void main(String[] args) { 26 | SpringApplication.run(JpaGisDemoApplication.class, args); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/config/JacksonConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.JtsModule; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 6 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 7 | import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; 8 | import com.github.lonelyleaf.gis.util.JsonUtil; 9 | import org.geolatte.geom.json.GeolatteGeomModule; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | 13 | /** 14 | * jackson配置 15 | */ 16 | @Configuration 17 | public class JacksonConfig { 18 | 19 | /* 20 | * Jackson Afterburner module to speed up serialization/deserialization. 21 | */ 22 | // @Bean 23 | // public AfterburnerModule afterburnerModule() { 24 | // AfterburnerModule module = new AfterburnerModule(); 25 | // // make Afterburner generate bytecode only for public getters/setter and fields 26 | // // without this, Java 9+ complains of "Illegal reflective access" 27 | // module.setUseValueClassLoader(false); 28 | // return module; 29 | // } 30 | 31 | @Bean 32 | public ParameterNamesModule parameterNamesModule() { 33 | return new ParameterNamesModule(); 34 | } 35 | 36 | @Bean 37 | public Jdk8Module jdk8Module() { 38 | return new Jdk8Module(); 39 | } 40 | 41 | @Bean 42 | public JavaTimeModule javaTimeModule() { 43 | return new JavaTimeModule(); 44 | } 45 | 46 | // @Bean 47 | // public ShapesAsGeoJSONModule shapesAsGeoJSONModule(){ 48 | // return new ShapesAsGeoJSONModule(); 49 | // } 50 | 51 | @Bean 52 | public JtsModule jtsModule() { 53 | return new JtsModule(); 54 | } 55 | 56 | @Bean 57 | public GeolatteGeomModule geolatteGeomModule() { 58 | return new GeolatteGeomModule(); 59 | } 60 | 61 | @Bean 62 | public JsonUtil jsonUtil(ObjectMapper objectMapper) { 63 | //只是把objectmapper注入 64 | JsonUtil.objectMapper = objectMapper; 65 | return new JsonUtil(); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/config/JpaConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2020] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.config; 18 | 19 | import com.querydsl.jpa.impl.JPAQueryFactory; 20 | import org.springframework.context.annotation.Bean; 21 | import org.springframework.context.annotation.Configuration; 22 | 23 | import javax.persistence.EntityManager; 24 | 25 | @Configuration 26 | public class JpaConfig { 27 | 28 | @Bean 29 | public JPAQueryFactory jpaQueryFactory(EntityManager entityManager){ 30 | return new JPAQueryFactory(entityManager); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.http.ResponseEntity; 9 | import springfox.documentation.builders.ApiInfoBuilder; 10 | import springfox.documentation.builders.PathSelectors; 11 | import springfox.documentation.builders.RequestHandlerSelectors; 12 | import springfox.documentation.service.ApiInfo; 13 | import springfox.documentation.spi.DocumentationType; 14 | import springfox.documentation.spring.web.plugins.Docket; 15 | import springfox.documentation.swagger.web.DocExpansion; 16 | import springfox.documentation.swagger.web.UiConfiguration; 17 | import springfox.documentation.swagger.web.UiConfigurationBuilder; 18 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 19 | 20 | /** 21 | * swagger配置,这里添加了基于jwt认证的代码 22 | */ 23 | @Configuration 24 | @EnableSwagger2 25 | @EnableConfigurationProperties(SwaggerProperties.class) 26 | @ConditionalOnProperty(value = "swagger.enable", matchIfMissing = true) 27 | public class SwaggerConfig { 28 | 29 | private final SwaggerProperties p; 30 | 31 | public SwaggerConfig(SwaggerProperties p) { 32 | this.p = p; 33 | } 34 | 35 | @Bean 36 | @ConditionalOnMissingBean(Docket.class) 37 | public Docket api() { 38 | Docket docket = new Docket(DocumentationType.SWAGGER_2); 39 | docket.select() 40 | .build() 41 | .pathMapping("/") 42 | .genericModelSubstitutes(ResponseEntity.class) 43 | .enableUrlTemplating(false); 44 | //@formatter:off 45 | return docket 46 | .apiInfo(apiInfo()) 47 | .useDefaultResponseMessages(false) 48 | .select() 49 | .paths(PathSelectors.any()) 50 | //基于包名显示文档 51 | .apis(RequestHandlerSelectors.basePackage(p.getBasePackage())) 52 | .build(); 53 | //@formatter:on 54 | } 55 | 56 | private ApiInfo apiInfo() { 57 | return new ApiInfoBuilder() 58 | .title("seed-demo api文档") 59 | .description("api文档描述,todo") 60 | .build(); 61 | } 62 | 63 | @Bean 64 | public UiConfiguration uiConfig() { 65 | return UiConfigurationBuilder.builder() 66 | .docExpansion(DocExpansion.NONE) 67 | .build(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/config/SwaggerProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * swagger配置参数 7 | */ 8 | @ConfigurationProperties(prefix = "swagger") 9 | public class SwaggerProperties { 10 | private Boolean enable = true; 11 | private String baseUrl = "/"; 12 | private String basePackage = "com.github.lonelyleaf.gis.rest"; 13 | 14 | public Boolean getEnable() { 15 | return enable; 16 | } 17 | 18 | public void setEnable(Boolean enable) { 19 | this.enable = enable; 20 | } 21 | 22 | public String getBaseUrl() { 23 | return baseUrl; 24 | } 25 | 26 | public void setBaseUrl(String baseUrl) { 27 | this.baseUrl = baseUrl; 28 | } 29 | 30 | public String getBasePackage() { 31 | return basePackage; 32 | } 33 | 34 | public void setBasePackage(String basePackage) { 35 | this.basePackage = basePackage; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/dto/GpsDto.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Data; 6 | 7 | import java.util.Date; 8 | 9 | @Data 10 | public class GpsDto { 11 | @JsonFormat(shape = JsonFormat.Shape.STRING, 12 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 13 | timezone = "+08") 14 | @ApiModelProperty("时间") 15 | private Date time; 16 | @ApiModelProperty("设备id") 17 | private String devId; 18 | @ApiModelProperty("位置") 19 | private SimplePoint location; 20 | @ApiModelProperty("卫星定位数") 21 | private int gpsNum; 22 | @ApiModelProperty("GPS定位信息") 23 | private String gpsType; 24 | @ApiModelProperty("对地真北航向角") 25 | private double azimuth; 26 | @ApiModelProperty("地面速率") 27 | private double gndRate; 28 | } 29 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/dto/GpsLine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.dto; 18 | 19 | import com.fasterxml.jackson.annotation.JsonFormat; 20 | import io.swagger.annotations.ApiModelProperty; 21 | import lombok.Data; 22 | import org.locationtech.jts.geom.LineString; 23 | 24 | import java.util.Date; 25 | 26 | @Data 27 | public class GpsLine { 28 | 29 | @JsonFormat(shape = JsonFormat.Shape.STRING, 30 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 31 | timezone = "+08") 32 | @ApiModelProperty("时间") 33 | private Date start; 34 | @JsonFormat(shape = JsonFormat.Shape.STRING, 35 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 36 | timezone = "+08") 37 | @ApiModelProperty("时间") 38 | private Date end; 39 | @ApiModelProperty("设备id") 40 | private String devId; 41 | 42 | private LineString line; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/dto/SimplePoint.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import org.locationtech.jts.geom.Point; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | @NoArgsConstructor 11 | public class SimplePoint { 12 | 13 | private double x; 14 | private double y; 15 | 16 | public SimplePoint(Point point) { 17 | this.x = point.getX(); 18 | this.y = point.getY(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/entity/DistrictEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.entity; 18 | 19 | 20 | import io.swagger.annotations.ApiModel; 21 | import io.swagger.annotations.ApiModelProperty; 22 | import lombok.AllArgsConstructor; 23 | import lombok.Data; 24 | import lombok.NoArgsConstructor; 25 | 26 | import javax.persistence.*; 27 | 28 | @Data 29 | @ApiModel("行政区,这里只有贵州的") 30 | @NoArgsConstructor 31 | @AllArgsConstructor 32 | @Entity 33 | @Table(name = "t_guizhou_boundary") 34 | public class DistrictEntity { 35 | 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.AUTO) 38 | private int gid; 39 | @ApiModelProperty("行政区名") 40 | private String name; 41 | @ApiModelProperty("行政区等级,省、市、区县、乡镇") 42 | private String layer; 43 | @ApiModelProperty("编码") 44 | private String code; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/entity/DistrictGeomEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.entity; 18 | 19 | 20 | import io.swagger.annotations.ApiModel; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | import org.locationtech.jts.geom.MultiPolygon; 24 | 25 | import javax.persistence.Entity; 26 | import javax.persistence.Table; 27 | 28 | @Data 29 | @EqualsAndHashCode(callSuper=true) 30 | @ApiModel("有行政边界的行政区") 31 | @Entity 32 | @Table(name ="t_guizhou_boundary") 33 | public class DistrictGeomEntity extends DistrictEntity { 34 | 35 | private MultiPolygon geom; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/entity/GpsEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Data; 6 | import org.geolatte.geom.G2D; 7 | import org.geolatte.geom.Point; 8 | 9 | import javax.persistence.*; 10 | import java.util.Date; 11 | 12 | @Data 13 | @Entity 14 | @Table(name = "t_gps") 15 | public class GpsEntity { 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.AUTO) 18 | private Integer gid; 19 | @JsonFormat(shape = JsonFormat.Shape.STRING, 20 | pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", 21 | timezone = "+08") 22 | @ApiModelProperty("时间") 23 | private Date time; 24 | @ApiModelProperty("设备id") 25 | private String devId; 26 | @ApiModelProperty("位置") 27 | private Point location; 28 | @ApiModelProperty("卫星定位数") 29 | private int gpsNum; 30 | @ApiModelProperty("GPS定位信息") 31 | private String gpsType; 32 | @ApiModelProperty("对地真北航向角") 33 | private double azimuth; 34 | @ApiModelProperty("地面速率") 35 | private double gndRate; 36 | } 37 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/mapper/GpsMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.mapper; 2 | 3 | 4 | import com.github.lonelyleaf.gis.dto.GpsDto; 5 | import com.github.lonelyleaf.gis.dto.SimplePoint; 6 | import com.github.lonelyleaf.gis.entity.GpsEntity; 7 | import com.github.lonelyleaf.gis.util.JtsUtil; 8 | import org.geolatte.geom.G2D; 9 | import org.locationtech.jts.geom.Coordinate; 10 | import org.locationtech.jts.geom.CoordinateXY; 11 | import org.locationtech.jts.geom.Point; 12 | import org.mapstruct.Mapper; 13 | import org.mapstruct.Mapping; 14 | import org.mapstruct.Mappings; 15 | 16 | import static org.geolatte.geom.builder.DSL.g; 17 | import static org.geolatte.geom.builder.DSL.point; 18 | 19 | @Mapper 20 | public interface GpsMapper { 21 | 22 | GpsDto toDto(GpsEntity entity); 23 | 24 | @Mappings({ 25 | @Mapping(ignore = true, target = "gid") 26 | }) 27 | GpsEntity toEntity(GpsDto entity); 28 | 29 | default SimplePoint toSimple(Point point) { 30 | return new SimplePoint(point); 31 | } 32 | 33 | default SimplePoint toSimple(org.geolatte.geom.Point point) { 34 | return new SimplePoint(point.getPosition().getLat(), point.getPosition().getLon()); 35 | } 36 | 37 | default org.geolatte.geom.Point toGeolattePoint(SimplePoint point) { 38 | G2D g = g(point.getX(), point.getY()); 39 | return point(JtsUtil.WGS84, g); 40 | } 41 | 42 | default Point toGisPoint(SimplePoint point) { 43 | Coordinate coordinate = new CoordinateXY(point.getX(), point.getY()); 44 | return JtsUtil.geometryFactory4326.createPoint(coordinate); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/repo/DistrictRepo.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.repo; 2 | 3 | 4 | import com.github.lonelyleaf.gis.entity.DistrictEntity; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.querydsl.QuerydslPredicateExecutor; 7 | 8 | /** 9 | * 行政区仓储 10 | */ 11 | public interface DistrictRepo extends JpaRepository, QuerydslPredicateExecutor { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/repo/GpsRepo.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.repo; 2 | 3 | 4 | import com.github.lonelyleaf.gis.entity.GpsEntity; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.querydsl.QuerydslPredicateExecutor; 7 | 8 | /** 9 | * GPS相关 10 | */ 11 | public interface GpsRepo extends JpaRepository, QuerydslPredicateExecutor { 12 | } 13 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/rest/DistrictController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.rest; 18 | 19 | import com.github.lonelyleaf.gis.service.DistrictService; 20 | import io.swagger.annotations.Api; 21 | import org.springframework.validation.annotation.Validated; 22 | import org.springframework.web.bind.annotation.RequestMapping; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | @RestController 26 | @RequestMapping("/district") 27 | @Validated 28 | @Api(description = "行政区与边界") 29 | public class DistrictController { 30 | 31 | private final DistrictService districtService; 32 | 33 | public DistrictController(DistrictService districtService) { 34 | this.districtService = districtService; 35 | } 36 | 37 | // @GetMapping 38 | // @ApiResponses( 39 | // {@ApiResponse(code = 200, message = "geom=false时,不会携带geom字段")} 40 | // ) 41 | // public List list( 42 | // @ApiParam("行政区名,支持like") 43 | // @RequestParam(name = "name", required = false) String name, 44 | // @ApiParam("层级,省,市,区县。多个用逗号分隔") 45 | // @RequestParam(name = "layers", required = false) String layersStr, 46 | // @ApiParam("是否携带geom对象,geom数据量很大,一般查询可以不携带") 47 | // @RequestParam(name = "geom", required = false, defaultValue = "false") boolean geom) { 48 | // List layers = Collections.emptyList(); 49 | // if (!Strings.isNullOrEmpty(layersStr)) { 50 | // layers = Arrays.asList(layersStr.split(",")); 51 | // } 52 | // return districtService.findBy(name, layers, geom); 53 | // } 54 | // 55 | // @GetMapping("/within") 56 | // public List within( 57 | // @ApiParam("点,x和y,逗号分隔") 58 | // @RequestParam(name = "point", required = false) String pointStr, 59 | // @ApiParam("是否携带geom对象,geom数据量很大,一般查询可以不携带") 60 | // @RequestParam(name = "geom", required = false, defaultValue = "false") boolean geom) { 61 | // String[] pointArray = pointStr.split(","); 62 | // Point point = JtsUtil.newPoint(Double.parseDouble(pointArray[0]), Double.parseDouble(pointArray[1])); 63 | // return districtService.withIn(point, geom); 64 | // } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/rest/GpsController.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.rest; 2 | 3 | import com.github.lonelyleaf.gis.dto.GpsDto; 4 | import com.github.lonelyleaf.gis.entity.GpsEntity; 5 | import com.github.lonelyleaf.gis.mapper.GpsMapper; 6 | import com.github.lonelyleaf.gis.service.GpsService; 7 | import com.google.common.base.Strings; 8 | import io.swagger.annotations.Api; 9 | import org.springframework.format.annotation.DateTimeFormat; 10 | import org.springframework.validation.annotation.Validated; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.Date; 14 | import java.util.List; 15 | import java.util.stream.Collectors; 16 | 17 | @RestController 18 | @RequestMapping("/gps") 19 | @Validated 20 | @Api(description = "轨迹数据") 21 | public class GpsController { 22 | 23 | private final GpsService gpsService; 24 | private final GpsMapper gpsMapper; 25 | 26 | public GpsController(GpsService gpsService, GpsMapper gpsMapper) { 27 | this.gpsService = gpsService; 28 | this.gpsMapper = gpsMapper; 29 | } 30 | 31 | @GetMapping("/history") 32 | public List history( 33 | @RequestParam(value = "devId", required = false) 34 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 35 | @RequestParam(value = "bTime", required = false) 36 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 37 | @RequestParam(value = "eTime", required = false) 38 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 39 | return gpsService.history(devId, bTime, eTime).stream() 40 | .map(gpsMapper::toDto) 41 | .collect(Collectors.toList()); 42 | } 43 | 44 | @GetMapping("/history/geojson") 45 | public List historyGeojson( 46 | @RequestParam(value = "devId", required = false) 47 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 48 | @RequestParam(value = "bTime", required = false) 49 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 50 | @RequestParam(value = "eTime", required = false) 51 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 52 | return gpsService.history(devId, bTime, eTime); 53 | } 54 | 55 | // @GetMapping("/line") 56 | // public GpsLine line( 57 | // @NotEmpty 58 | // @RequestParam(value = "devId", required = false) 59 | // @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 60 | // @RequestParam(value = "bTime", required = false) 61 | // @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 62 | // @RequestParam(value = "eTime", required = false) 63 | // @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 64 | // 65 | // List history = gpsService.history(devId, bTime, eTime); 66 | // GpsLine gpsLine = new GpsLine(); 67 | // gpsLine.setDevId(devId); 68 | // gpsLine.setStart(bTime); 69 | // gpsLine.setEnd(eTime); 70 | // 71 | // Coordinate[] points = history.stream() 72 | // .map(entity -> entity.getLocation().getCoordinate()) 73 | // .toArray(Coordinate[]::new); 74 | // 75 | // gpsLine.setLine(JtsUtil.geometryFactory4326.createLineString(points)); 76 | // return gpsLine; 77 | // } 78 | 79 | // @GetMapping("/history/simple") 80 | // public List simpleHistory( 81 | // @RequestParam(value = "devId", required = false) 82 | // @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) String devId, 83 | // @RequestParam(value = "bTime", required = false) 84 | // @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date bTime, 85 | // @RequestParam(value = "eTime", required = false) 86 | // @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date eTime) { 87 | // List history = gpsService.history(devId, bTime, eTime); 88 | // return history.stream() 89 | // .map(gpsEntity -> new SimplePoint(gpsEntity.getLocation())) 90 | // .collect(Collectors.toList()); 91 | // } 92 | 93 | @PostMapping 94 | public String simpleHistory(@RequestBody GpsDto gpsDto) { 95 | if (gpsDto.getTime() == null) { 96 | gpsDto.setTime(new Date()); 97 | } 98 | if (Strings.isNullOrEmpty(gpsDto.getDevId())) { 99 | throw new IllegalArgumentException("devId不能为空"); 100 | } 101 | 102 | GpsEntity entity = gpsMapper.toEntity(gpsDto); 103 | gpsService.save(entity); 104 | return "ok"; 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/service/DistrictService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.service; 18 | 19 | import com.github.lonelyleaf.gis.repo.DistrictRepo; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.stereotype.Service; 22 | 23 | @Service 24 | public class DistrictService { 25 | 26 | @Autowired 27 | DistrictRepo districtRepo; 28 | 29 | // @SuppressWarnings("unchecked") 30 | // public List findBy(String name, List layers, boolean geom) { 31 | // QueryWrapper wrapper = new QueryWrapper<>(); 32 | // if (!Strings.isNullOrEmpty(name)) { 33 | // wrapper.like("name", name); 34 | // } 35 | // if (layers != null && layers.size() != 0) { 36 | // wrapper.in("layer", layers); 37 | // } 38 | // 39 | // if (geom) { 40 | // return districtGeomRepo.selectList((Wrapper) wrapper); 41 | // } else { 42 | // return list((Wrapper) wrapper); 43 | // } 44 | // } 45 | // 46 | // public List withIn(Point point, boolean geom) { 47 | // PostgisWrapper wrapper = new PostgisWrapper<>(); 48 | // wrapper.stDWithIn(point, "geom", 5); 49 | // 50 | // if (geom) { 51 | // return districtGeomRepo.selectList((Wrapper) wrapper); 52 | // } else { 53 | // return list((Wrapper) wrapper); 54 | // } 55 | // } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/service/GpsService.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.service; 2 | 3 | import com.github.lonelyleaf.gis.entity.GpsEntity; 4 | import com.github.lonelyleaf.gis.repo.GpsRepo; 5 | import com.google.common.collect.Lists; 6 | import com.querydsl.core.types.ExpressionUtils; 7 | import com.querydsl.core.types.Predicate; 8 | import com.querydsl.jpa.impl.JPAQueryFactory; 9 | import org.locationtech.jts.geom.Point; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import javax.validation.constraints.NotNull; 14 | import java.util.Collections; 15 | import java.util.Date; 16 | import java.util.List; 17 | 18 | import static com.github.lonelyleaf.gis.entity.QGpsEntity.gpsEntity; 19 | 20 | @Service 21 | public class GpsService { 22 | 23 | @Autowired 24 | GpsRepo gpsRepo; 25 | 26 | public List history(String devId, Date bTime, Date eTime) { 27 | Predicate predicate = gpsEntity.devId.eq(devId); 28 | if (bTime != null) { 29 | predicate = ExpressionUtils.and(predicate, gpsEntity.time.gt(bTime)); 30 | } 31 | if (eTime != null) { 32 | predicate = ExpressionUtils.and(predicate, gpsEntity.time.lt(eTime)); 33 | } 34 | return Lists.newArrayList(gpsRepo.findAll(predicate)); 35 | } 36 | 37 | @Autowired 38 | JPAQueryFactory jpaQueryFactory; 39 | 40 | public List dWithIn(@NotNull Point point, double distance) { 41 | return Collections.emptyList(); 42 | // jpaQueryFactory.select(gpsEntity).dis 43 | // new JPAQuery() 44 | // gpsEntity.lo 45 | // Predicate predicate = gpsEntity.devId.eq(devId); 46 | // if (bTime != null) { 47 | // predicate = ExpressionUtils.and(predicate, gpsEntity.time.gt(bTime)); 48 | // } 49 | // if (eTime != null) { 50 | // predicate = ExpressionUtils.and(predicate, gpsEntity.time.lt(eTime)); 51 | // } 52 | // return Lists.newArrayList(gpsRepo.findAll(predicate)); 53 | } 54 | 55 | public void save(GpsEntity entity) { 56 | entity.setGid(null); 57 | gpsRepo.save(entity); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/util/JsonUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis.util; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | 6 | import java.io.IOException; 7 | 8 | /** 9 | * 方便序列化json 10 | */ 11 | public class JsonUtil { 12 | 13 | public static ObjectMapper objectMapper = new ObjectMapper() 14 | .registerModules(ObjectMapper.findModules()); 15 | 16 | public static String toJson(Object obj) { 17 | try { 18 | return objectMapper.writeValueAsString(obj); 19 | } catch (JsonProcessingException e) { 20 | throw new RuntimeException(e); 21 | } 22 | } 23 | 24 | public static String toPrettyJson(Object obj) { 25 | try { 26 | return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); 27 | } catch (JsonProcessingException e) { 28 | throw new RuntimeException(e); 29 | } 30 | } 31 | 32 | public static T fromJson(String s, Class clazz) { 33 | try { 34 | return objectMapper.readValue(s, clazz); 35 | } catch (IOException e) { 36 | throw new RuntimeException(e); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /03-jpa/src/main/java/com/github/lonelyleaf/gis/util/JtsUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2019] [lonelyleaf] 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.lonelyleaf.gis.util; 18 | 19 | 20 | import org.geolatte.geom.crs.CoordinateReferenceSystems; 21 | import org.geolatte.geom.crs.Geographic2DCoordinateReferenceSystem; 22 | import org.locationtech.jts.geom.CoordinateXY; 23 | import org.locationtech.jts.geom.GeometryFactory; 24 | import org.locationtech.jts.geom.Point; 25 | import org.locationtech.jts.geom.PrecisionModel; 26 | 27 | public class JtsUtil { 28 | 29 | /** 30 | * srid为4326 31 | */ 32 | public static final GeometryFactory geometryFactory4326 = new GeometryFactory(new PrecisionModel(), 4326); 33 | 34 | public static Point newPoint(double x, double y) { 35 | return geometryFactory4326.createPoint(new CoordinateXY(x, y)); 36 | } 37 | 38 | 39 | // public static CoordinateReferenceSystem c4326 = CrsRegistry.getProjectedCoordinateReferenceSystemForEPSG(4326); 40 | // public static CoordinateReferenceSystem g4326 = CrsRegistry.getGeographicCoordinateReferenceSystemForEPSG(4326); 41 | public static Geographic2DCoordinateReferenceSystem WGS84 = CoordinateReferenceSystems.WGS84; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | jackson: 5 | serialization: 6 | write_dates_as_timestamps: true 7 | 8 | servlet: 9 | multipart: 10 | max-file-size: 10MB 11 | 12 | jdbc: 13 | template: 14 | query-timeout: 15 15 | 16 | flyway: 17 | enabled: true 18 | 19 | jpa: 20 | hibernate: 21 | ddl-auto: none 22 | properties: 23 | hibernate: 24 | show_sql: false 25 | dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect 26 | database: postgresql 27 | 28 | datasource: 29 | driver-class-name: org.postgresql.Driver 30 | url: jdbc:postgresql://localhost:5432/jpa_test 31 | #这里使用的驱动不一样!,可以自动添加jts的各种类型 32 | # driver-class-name: org.postgis.jts.JtsWrapper 33 | # #url也不一样 34 | # url: jdbc:postgres_jts://localhost:5432/jpa-test 35 | username: postgres 36 | password: postgres 37 | initialization-mode: never 38 | 39 | mybatis-plus: 40 | # configuration: 41 | # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 42 | type-handlers-package: "com.github.lonelyleaf.gis.mybatis.jts" 43 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.0__init.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:e45a6e0bf06062049603c05047e5d08a3ca55f276591192794fa9312cc2bc2c2 3 | size 713 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.1__gps_sample_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:2dfbaa6ebc8fdc42a87930d988634729a2ded58098a1bc966b6ae95e92ce13df 3 | size 1647024 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.2__index_gps.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:236c5272d6bfffc4f572158e160823f87dfb09108a3779429dc96bd02ddda471 3 | size 125 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.3__guizhou_boundary.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:6e82eb84e3d82738a32eb3d8320e6e4e083d60d13e43e1ee6bce24979dc0c238 3 | size 948 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.4__guizhou_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:33923dc849844cd9f2fed4aa8d453bf396d9b534a6878669cd901945cdcb306a 3 | size 5853288 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.5__guizhou_index.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f101e0687d5c1d6da7297668b1cb9c66af99e0f74844c272fe8f91476b9716ec 3 | size 773 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.6__zunyi_roads.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:facabc9a1f2a723be09d795b90f6a2ee515ff77633568424d730ebe8fbef3613 3 | size 912 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.7__zunyi_roads_data.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:579877976bbcdf09bcaa3f78310b8a2f29b7ccb1a6e964b84d69e71c5ba08646 3 | size 8081799 4 | -------------------------------------------------------------------------------- /03-jpa/src/main/resources/db/migration/V1.8__zunyi_roads_index.sql: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:240360354d3869ae4cd7624c45ce30db3a1af1da8b12ce0b56bc331baf5b3820 3 | size 764 4 | -------------------------------------------------------------------------------- /03-jpa/src/test/java/com/github/lonelyleaf/gis/TestGeoJson.java: -------------------------------------------------------------------------------- 1 | package com.github.lonelyleaf.gis; 2 | 3 | import com.bedatadriven.jackson.datatype.jts.JtsModule; 4 | import com.fasterxml.jackson.core.JsonProcessingException; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.junit.jupiter.api.Test; 8 | import org.locationtech.jts.geom.Coordinate; 9 | import org.locationtech.jts.geom.GeometryFactory; 10 | import org.locationtech.jts.geom.Point; 11 | 12 | @Slf4j 13 | class TestGeoJson { 14 | 15 | @Test 16 | public void test() throws JsonProcessingException { 17 | ObjectMapper mapper = new ObjectMapper(); 18 | mapper.registerModule(new JtsModule()); 19 | 20 | 21 | GeometryFactory gf = new GeometryFactory(); 22 | Point point = gf.createPoint(new Coordinate(1.2345678, 2.3456789)); 23 | String geojson = mapper.writeValueAsString(point); 24 | log.info(geojson); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright [2019] [lonelyleaf] 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # postgis-java-demo 2 | 3 | 使用spring boot+mybatis对postgis进行基本增删改查操作。 4 | 5 | 6 | ## 1.环境说明 7 | 8 | - jdk11 9 | - gradle6 10 | - postgres11+postgis 11 | - idea 2019.3 12 | 13 | 14 | ## 2.项目结构说明 15 | 16 | - 项目基于spring boot搭建 17 | - 数据库层使用的[mybatis-plus]而不是直接使用mybatis,但基本配置差别不大 18 | - 使用了lombok 19 | - 使用了flyway来管理数据库表 20 | - 使用了[mapstruct]来做dto转换 21 | - 使用了[postgis-jdbc]来做postgis与java的类型转换 22 | - 使用了[spring-fox]来自动文档生成 23 | 24 | ## 3.启动项目 25 | 26 | 首先需要安装好postgres与postgis。 27 | 然后创建数据库,这里叫做`gis-test` 28 | 29 | 进入数据库后启用postgis 30 | 31 | > CREATE EXTENSION postgis 32 | 33 | 之后在ide中启动项目,就会自动通过flyway把表结构与样本数据自动初始化好了。 34 | 35 | 启动后通过`http://localhost:8080/swagger-ui.html`就可以访问文档并调用rest接口了。 36 | 37 | ## 4.注意事项 38 | 39 | 40 | ##### 4.1. geography的处理 41 | 42 | [postgis-jdbc]的文档中,对使用有说明 43 | ``` 44 | - You can use the org.postgis.DriverWrapper as replacement for the 45 | jdbc driver. This class wraps the PostGreSQL Driver to 46 | transparently add the PostGIS Object Classes. This method currently 47 | works both with J2EE DataSources, and with the older DriverManager 48 | framework. I's a thin wrapper around org.postgresql.Driver that 49 | simply registers the extension on every new connection created. 50 | 51 | To use it, you replace the "jdbc:postgresql:" with a 52 | "jdbc:postgresql_postGIS" in the jdbc URL, and make your 53 | environment aware of the new Driver class. 54 | ``` 55 | 56 | 也就是要使用`org.postgis.DriverWrapper`作为driver,然后jdbc的url使用`jdbc:postgresql_postGIS`就可以 57 | 自动注册`org.postgis.Geometry`。但是,源码中支持的数据库类型是`geometry`而不支持`geography`,这两者的 58 | 差别可以参考`不睡觉的怪叔叔`和`德哥`的文章 59 | 60 | [PostGIS教程十三:地理](https://blog.csdn.net/qq_35732147/article/details/86489918) 61 | [PostGIS 距离计算建议 - 投影 与 球 坐标系, geometry 与 geography 类型](https://github.com/digoal/blog/blob/master/201710/20171018_02.md) 62 | 63 | 64 | 为了让`geography`也能自动注册,项目中自定义了`com.github.lonelyleaf.gis.db.DriverWrapper`类, 65 | 转换出来还是`org.postgis.Geometry`,在java代码层使用和`geometry`并没做区分。 66 | 67 | ##### 4.2. org.postgis.Geometry的json序列化 68 | 69 | 由于没有现成的`org.postgis.Geometry`转为[GeoJson]的库,所以项目中使用了自定义`SimplePoint`类来 70 | 转换以下然后序列化。 71 | 72 | 其实[postgis-jdbc]中有`org.postgis.jts`包,有对[JTS]的支持,[JTS]是有jackson库转[GeoJson]的。 73 | 如果需要在java层做些地理位置的运算,使用`org.postgis.jts`包应该更好。 74 | 75 | ##### 4.3. 坐标系的选择 76 | 77 | SRID的选择其实很复杂,详细解释可以参考下`不睡觉的怪叔叔`的文章https://blog.csdn.net/qq_35732147/article/details/86301242。 78 | 这里摘抄一段 79 | 80 | ``` 81 | 地球不是平的,也没有简单的方法把它放在一张平面纸地图上(或电脑屏幕上),所以人们想出了各种巧妙的解决方案(投影)。 82 | 83 | 每种投影方案都有优点和缺点,一些投影保留面积特征;一些投影保留角度特征,如墨卡托投影(Mercator); 84 | 一些投影试图找到一个很好的中间混合状态,在几个参数上只有很小的失真。所有投影的共同之处在于, 85 | 它们将(地球)转换为平面笛卡尔坐标系,选择哪种投影取决于你将如何使用数据(需要哪些数据特征,面积?角度?或者其他)。 86 | ``` 87 | 88 | [SRID]其实就决定了你的坐标使用的哪种投影,由于我的数据都是标准的gps坐标(经纬度,没有偏移), 89 | 所以在转换与建表时,都使用了`srid=4326`。具体数据库应该使用哪种一定要根据业务来,不然使用postgis进行计算与使用 90 | 各种gis软件进行分析时一定会出问题。 91 | 92 | 在转换到`org.postgis.Point`时,项目中写死了srid的值,这不是必须,但为了保证正确最好这样做: 93 | ``` 94 | default org.postgis.Point toGisPoint(SimplePoint point) { 95 | Point gisPoint = new Point(); 96 | gisPoint.x = point.getX(); 97 | gisPoint.y = point.getY(); 98 | gisPoint.dimension = 2; 99 | //WGS84坐标系,也就是GPS使用的坐标 100 | gisPoint.srid = 4326; 101 | return gisPoint; 102 | } 103 | ``` 104 | 105 | 106 | [mybatis-plus]: https://mp.baomidou.com/ 107 | [mapstruct]: https://mapstruct.org/ 108 | [postgis-jdbc]: https://github.com/postgis/postgis-java 109 | [spring-fox]: https://github.com/springfox/springfox 110 | [GeoJson]: https://geojson.org/ 111 | [JTS]: https://github.com/locationtech/jts 112 | [SRID]: https://en.wikipedia.org/wiki/Spatial_reference_system -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | //相当于maven的properties,这里是全局的变量 3 | ext { 4 | springBootVersion = '2.4.1' 5 | springFoxVersion = "3.0.0" 6 | logstashLogbackEncoderVersion = "6.1" 7 | 8 | mybatisPlusVersion = "3.3.0" 9 | postgresJdbcVersion = "42.2.18" 10 | 11 | mapstructVersion = "1.4.1.Final" 12 | 13 | postgisJdbcVersion = "2.5.0" 14 | jtsVersion = "1.17.1" 15 | spatial4jVersion = "0.8" 16 | geolatteGeomVersion = "1.6.0" 17 | } 18 | //gradle插件的仓库 19 | repositories { 20 | maven { 21 | url "http://maven.aliyun.com/nexus/content/groups/public/" 22 | } 23 | jcenter() 24 | maven { 25 | url "https://plugins.gradle.org/m2/" 26 | } 27 | maven { url "https://jitpack.io" } 28 | mavenCentral() 29 | } 30 | //插件引用 31 | dependencies { 32 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 33 | classpath "io.freefair.gradle:lombok-plugin:4.1.5" 34 | } 35 | } 36 | 37 | allprojects{ 38 | group = 'xyz.lonelyleaf' 39 | version = '0.0.1-SNAPSHOT' 40 | 41 | repositories { 42 | maven { 43 | url "http://maven.aliyun.com/nexus/content/groups/public/" 44 | } 45 | jcenter() 46 | maven { url "https://jitpack.io" } 47 | mavenCentral() 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lonelyleaf/postgis-java-demo/08ffd0ab6c75b6730e5fda1f7345b53ce973f765/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 09 10:25:29 CST 2019 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /lombok.config: -------------------------------------------------------------------------------- 1 | # This file is generated by the 'io.freefair.lombok' Gradle plugin 2 | config.stopBubbling = true 3 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'postgis-java-demo' 2 | 3 | include "00-jackson-datatype-jts" 4 | include "00-postgis-jdbc-jts" 5 | include "01-postgis" 6 | include "02-jts" 7 | include "03-jpa" --------------------------------------------------------------------------------