• geotools实现多边形的合并&缓冲区


    这算是第一次接触开源工具包,说实话刚开始有点不知所措,中途遇到很多问题的时候也感觉头皮发麻,不过很高兴自己还是坚持下来了。

    geotools就不做过多的介绍了,想总结一下如何根据开源内容做自己的项目。整个过程中技术相关的内容学到了不少,但要是都写出来篇幅就太长了,所以这篇主要是写一些自己总结的开发流程上的东西。

    一、 对要参考的开源项目有一个宏观的把握

    geotools是一个 GIS 开源工具包,官网有入门教程和一系列的参考文档,这些东西是非常关键的,要想把geotools里面的各个jar包的作用弄明白,就要仔细的阅读这些文档。geotools的 Quickstart 讲的是如何打开一个.shp文件, eclipse版本、IDEA版本的都有。Document有Featur(要素)教程,Geometry(几何)教程,Query(查询)教程等,要注意的是最的新版本可能不支持旧版里的一些内容。

    有篇博客讲了geotools的体系,非常值得参考:http://blog.csdn.net/anglestar2012/article/details/42555819

    由于我要做的与几何内容联系比较大,所以着重参考了JTS Topology Suite(拓扑套件)类库,这里面的类主要是用来实现具体的几何操作,主要用到了以下三个包:

    org.locationtech.jts.operation.union

    org.locationtech.jts.operation.buffer

    org.locationtech.jts.algorithm  (PointLocator)

    附上JTS的文档地址:http://locationtech.github.io/jts/javadoc/overview-summary.html

    二、多看例子

    一周多的时间,我几乎翻遍了网上所有与geotools有关的博客,之后试着对里面的代码进行理解和仿写。这个阶段获取的信息量非常大,有很多重复的内容,关键是要从中提取出有用的信息点。

    三、分析自己项目的具体需求

    刚开始领导布置任务的时候是说要导入JSON格式的数据,所以又看了相关的一些知识,包括JSON格式的特点、使用范围以及如何通过Javajavascript对它进行解析与生成。

    三、下载、导入jar包,配置maven

    这个项目对我来讲非常重要的一点就是学会了maven工具的使用,这个构建工具非常强大,geotools就是默认使用maven来做的。maven重点在于对pom.xml文件的配置,导入jar包后就要添加依赖。

    这里附一个讲解地址:http://blog.csdn.net/u012152619/article/category/6239920

    下面是我配置的pom文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>org.geotools</groupId>
      <artifactId>buffer</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
      <name>buffer</name>
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>19-SNAPSHOT</geotools.version>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-main</artifactId>
          <version>${geotools.version}</version>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-shapefile</artifactId>
          <version>${geotools.version}</version>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-swing</artifactId>
          <version>${geotools.version}</version>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-api</artifactId>
          <version>${geotools.version}</version>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-epsg-hsql</artifactId>
          <version>${geotools.version}</version>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-geometry</artifactId>
          <version>${geotools.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-jts-wrapper</artifactId>
          <version>${geotools.version}</version>
        </dependency>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-geojson</artifactId>
          <version>${geotools.version}</version>
        </dependency>
      </dependencies>
      <repositories>
        <repository>
          <id>maven2-repository.dev.java.net</id>
          <name>Java.net repository</name>
          <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
          <id>osgeo</id>
          <name>Open Source Geospatial Foundation Repository</name>
          <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>boundless</id>
          <name>Boundless Maven Repository</name>
          <url>http://repo.boundlessgeo.com/main</url>
        </repository>
      </repositories>
    
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

    四、具体实现代码

    以前一直在用eclipse,这次做完之后把所有内容都迁到了IDEA,熟悉新IDE也花费了不少时间。二者我觉得都很好,目前的水平也感觉不出来IDEA有网上说的那么神奇,从体验来讲只是UI上的差异,不过有一点很重要,就是IDEA的结构,它没有eclipse中Workspace的概念,在IDEA中,Project是最顶级的结构单元,然后是Module,这类项目一般按照功能划分。

    这里有相关的介绍:http://blog.csdn.net/qq_35246620/article/details/65448689

    package org.geotools;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.geotools.geometry.jts.JTSFactoryFinder;
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.Geometry;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.io.WKTReader;
    import com.vividsolutions.jts.algorithm.PointLocator;
    
    public class UnionPolygon {
        public static void main(String[] args) {
            //创建4个多边形
            Coordinate[] coordinates1 = new Coordinate[] {
                    new Coordinate(0, 0),
                    new Coordinate(0, 1),
                    new Coordinate(1, 2),
                    new Coordinate(2, 1), //该点在union后会被消去
                    new Coordinate(2, 0),
                    new Coordinate(0, 0), };
            Coordinate[] coordinates2 = new Coordinate[] {
                    new Coordinate(1, 0),
                    new Coordinate(1, 1), //该点在union后会被消去
                    new Coordinate(2, 2),
                    new Coordinate(3, 1),
                    new Coordinate(3, 0),
                    new Coordinate(1, 0)};
            Coordinate[] coordinates3 = new Coordinate[] {
                    new Coordinate(0, 0),
                    new Coordinate(1, 0),
                    new Coordinate(2, 0),
                    new Coordinate(2, -1),
                    new Coordinate(0, -1),
                    new Coordinate(0, 0), };
            Coordinate[] coordinates4 = new Coordinate[] {
                    new Coordinate(1, 0),
                    new Coordinate(2, 0),
                    new Coordinate(3, 0),
                    new Coordinate(3, -1),
                    new Coordinate(1, -1),
                    new Coordinate(1, 0)};
    
            GeometryFactory gf=new GeometryFactory();
            Geometry g1 = gf.createPolygon(coordinates1);
            Geometry g2 = gf.createPolygon(coordinates2);
            Geometry g3 = gf.createPolygon(coordinates3);
            Geometry g4 = gf.createPolygon(coordinates4);
            //两个合并
            Geometry union = g1.union(g2);
            Geometry union2 = union.union(g3);
            //多个合并,设置多边形数组,再通过循环依次叠加各个多边形
            Geometry[] geos=new Geometry[] {g1,g2,g3,g4};
            Geometry allunion = geos[0];
            for(int i=1; i<geos.length; i++) {
                allunion=allunion.union(geos[i]);
            }
            System.out.println(union);
            System.out.println(union2);
            System.out.println(allunion);
    
            //缓冲区建立
            Geometry g3buffer=g3.buffer(1);                 //对第三个多边形加缓冲区
            Geometry allunionbuffer=allunion.buffer(1);     //对全部合并后的多边形加缓冲区
            System.out.println(g3buffer);
            System.out.println(allunionbuffer);
    
            //点是否在多边形内判断
            Coordinate point1 = new Coordinate(1, 1);
            PointLocator a=new PointLocator();
            boolean p1=a.intersects(point1, allunion);
            if(p1)
                System.out.println("point1:"+"该点在多边形内");
            else
                System.out.println("point1:"+"该点不在多边形内");
    
            Coordinate point2 = new Coordinate(5, 5);
            PointLocator b=new PointLocator();
            boolean p2=b.intersects(point2, allunion);
            if(p2)
                System.out.println("point2:"+"该点在多边形内");
            else
                System.out.println("point2:"+"该点不在多边形内");
        }
    }

     五、小总结

    做项目的提升速度其实比纯看书是快很多的,因为会碰到很多问题。我的方法是把一天遇到的与内容相关的问题点都记下来,然后再看它们分别属于什么知识范畴,最后安排时间去查阅相关的知识,重要的(比如maven)就按照体系来学习,进行宏观把握,其他的达到能看懂、能理解、会基础操作的水平就行。还有就是多交流吧,能解决很多问题。

    说到底,学习就是一个发现问题解决问题的过程,只不过这中间不同人选取的手段方法有一些差异而已。

    附   shp转geojson网址:http://mapshaper.org/

           地图数据:http://www.osgeo.cn/filter/2102

                             https://www.hcharts.cn/mapdata

     

  • 相关阅读:
    Web开发模式演变(转)
    面向切面编程--AOP(转)
    Python装饰器与面向切面编程(转)
    python 线程,GIL 和 ctypes(转)
    Python性能鸡汤(转)
    对Django框架架构和Request/Response处理流程的分析(转)
    django源码笔记-【2】(转)
    django源码笔记-【1】(转)
    PHPCMS9.6.0最新版SQL注入和前台GETSHELL漏洞分析 (实验新课)
    从零开始学习渗透Node.js应用程序
  • 原文地址:https://www.cnblogs.com/sleeping-dog/p/8365260.html
Copyright © 2020-2023  润新知