概述

本文讲述如何在geotools中实现蜂巢效果。

效果

实现

1.扩展类IntersectionBuilder

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import java.io.IOException;
import java.util.Map;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.grid.GridElement;
import org.geotools.grid.GridFeatureBuilder;
import org.geotools.grid.PolygonElement;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;public class IntersectionBuilder extends GridFeatureBuilder {final FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2();final GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();final SimpleFeatureSource source;int id = 0;public IntersectionBuilder(SimpleFeatureType type, SimpleFeatureSource source) {super(type);this.source = source;}public void setAttributes(GridElement el, Map<String, Object> attributes) {attributes.put("id", ++id);}@Overridepublic boolean getCreateFeature(GridElement el) {Coordinate c = ((PolygonElement) el).getCenter();Geometry p = gf.createPoint(c);Filter filter = ff2.intersects(ff2.property("the_geom"), ff2.literal(p));boolean result = false;try {result = !source.getFeatures(filter).isEmpty();} catch (IOException ex) {throw new IllegalStateException(ex);}return result;}
}

2.实现类HexGrid


import com.lzugis.geotools.utils.IntersectionBuilder;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.grid.Envelopes;
import org.geotools.grid.GridFeatureBuilder;
import org.geotools.grid.Grids;
import org.geotools.grid.hexagon.HexagonOrientation;
import org.geotools.grid.hexagon.Hexagons;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class HexGrid {/*** 保存为shp文件* @param filepath* @param sfsGrid*/public void writeShape(String filepath, SimpleFeatureSource sfsGrid) {try {//创建shape文件对象File file = new File(filepath);Map<String, Serializable> params = new HashMap<String, Serializable>();params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);//定义图形信息和属性信息SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();tb.setCRS(DefaultGeographicCRS.WGS84);tb.setName("shapefile");tb.add("the_geom", Polygon.class);tb.add("id", Integer.class);ds.createSchema(tb.buildFeatureType());//设置编码Charset charset = Charset.forName("GBK");ds.setCharset(charset);//设置WriterFeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);SimpleFeatureCollection resultgrid = sfsGrid.getFeatures();SimpleFeatureIterator itertorgrid = resultgrid.features();while (itertorgrid.hasNext()) {SimpleFeature feature = itertorgrid.next();SimpleFeature featureBuf = writer.next();featureBuf.setAttributes(feature.getAttributes());}writer.write();writer.close();ds.dispose();}catch (Exception e) {e.printStackTrace();}}/*** 生成蜂巢* @param shpfile* @param sideLen* @return*/public SimpleFeatureSource createGrid(String shpfile, float sideLen){try {File file = new File(shpfile);ShapefileDataStore shpDataStore = null;shpDataStore = new ShapefileDataStore(file.toURL());//设置编码Charset charset = Charset.forName("GBK");shpDataStore.setCharset(charset);String typeName = shpDataStore.getTypeNames()[0];SimpleFeatureSource featureSource = null;featureSource = shpDataStore.getFeatureSource(typeName);// Set the grid size (1 degree) and create a bounding envelope// that is neatly aligned with the grid sizeReferencedEnvelope gridBounds = Envelopes.expandToInclude(featureSource.getBounds(), sideLen);// Create a feature typeSimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();tb.setName("grid");tb.add(GridFeatureBuilder.DEFAULT_GEOMETRY_ATTRIBUTE_NAME,Polygon.class,gridBounds.getCoordinateReferenceSystem());tb.add("id", Integer.class);SimpleFeatureType TYPE = tb.buildFeatureType();// Build the grid the custom feature builder classGridFeatureBuilder builder = new IntersectionBuilder(TYPE, featureSource);SimpleFeatureSource grid =Hexagons.createGrid(gridBounds, sideLen, HexagonOrientation.ANGLED, builder);return grid;}catch (Exception e){e.printStackTrace();return null;}}public static void main(String[] args) throws Exception{long start = System.currentTimeMillis();HexGrid hexGrid = new HexGrid();String path = "D:\\data\\beijing\\";String shpfile = path+"beijing.shp";String gridFile = path+"bj_grid.shp";float sideLen = 0.03f;hexGrid.writeShape(gridFile, hexGrid.createGrid(shpfile, sideLen));System.out.println("共耗时"+(System.currentTimeMillis() - start)+"ms");}
}

技术博客
CSDN:http://blog.csdn.NET/gisshixisheng
在线教程
https://edu.csdn.net/course/detail/799
https://edu.csdn.net/course/detail/7471
联系方式

类型 内容
qq 1004740957
公众号 lzugis15
e-mail niujp08@qq.com
webgis群 452117357
Android群 337469080
GIS数据可视化群 458292378

“GIS讲堂”知识星球开通了,在星球,我将提供一对一的问答服务,你问我答,期待与你相见。

Geotools中蜂巢的实现相关推荐

  1. 解决GeoTools中CQL解析中文字段名的问题

    GeoTools中CQL无法解析中文字段名的过滤条件,会报异常错误,经过一个下午的努力,终于通过简单有效的方式解决啦 String filterCondition = "temp='&quo ...

  2. geotools中等值面的生成与OL3中的展示

    概述: 本文讲述如何在geotools中IDW插值生成等值面,并根据给定shp进行裁剪,并生成geojson数据,以及Openlayers3中展示. 效果: 插值数据 裁剪结果 裁剪区域数据 实现代码 ...

  3. GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换

    GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...

  4. GeoTools中的空间关系(Geometry Relationships)和空间操作(Geometry Operations)

    参考原文:GeoTools中的空间关系和空间操作 一.GeoTools包含的空间关系和空间操作: 空间关系(Geometry Relationships): 常见的空间关系(Geometry Rela ...

  5. java操作geotiff_关于java使用geotools中的GeoTiffReader读取tif图像时报错

    关于java使用geotools中的GeoTiffReader读取tif图像时报错:找不到类 javax.media.jai.PlanarImage 的错误 代码放上: import java.io. ...

  6. Geotools中实现NC转等值面

    概述: 前面的文章有实现IDW插值并生成等值面的,本文在前文基础上实现气象NC数据生成等值面. 效果: Arcgis预览图 实现后 3.代码 package com.lzugis.netcdf;imp ...

  7. GeoTools——JTS空间操作

    目录 一.引言 二.代码操作 1.服务端 2.返回数据 3.客户端 三.总结 一.引言 使用geotools主要是对数据进行操作,这里的操作包括空间关系判断和空间关系运算.这里的空间关系判断常用的是否 ...

  8. GeoTools——读取shapefile数据

    目录 一.引言 二.代码操作 1.服务端 2.返回数据 3.客户端 三.总结 一.引言 GeoTools在开源gis世界中使用极为常见,地位类比于arcgis中的arcgis engine,当我们要使 ...

  9. android仿IT之家、炫酷水波纹、Kotlin MVP项目、后台模拟点击、蜂巢迷宫小游戏等源码...

    Android精选源码 Android 炫酷的多重水波纹源码 Android开发一款基于行为识别和个性化推荐的智能推荐APP 仿IT之家Android源码 android判断App位于前台或者后台源码 ...

最新文章

  1. 借助JRebel使Tomcat支持热部署
  2. MySQL的explain工具介绍
  3. 2016matlab安装
  4. 父亲节重读朱自清先生的《背影》
  5. python pip安装第三方库版本问题_python使用pip安装第三方库的踩坑记录
  6. MTK 驱动(47)---使用PWM配置背光如何配置,及频率计算
  7. reentrantlock原理_你必须要知道的热门 ReentrantLock 及 AQS 的实现原理
  8. Android下拉刷新上拉更多瀑布流(附源码)
  9. Helm 架构 - 每天5分钟玩转 Docker 容器技术(161)
  10. pycharm项目中如何安装包_如何将Thymeleaf技术集成到SpringBoot项目中
  11. java生成KML文件
  12. ADS仿真目标参数调谐与目标优化 ----tuner和goal控件使用
  13. 白话布隆过滤器BloomFilter
  14. Python爬虫实战-小说网站爬虫开发
  15. H5标签在页面被转译
  16. Vue 事件处理 -- 事件修饰符(prevent、stop、capture、self、once)
  17. 未转变者服务器物品ID大全,Unturned未转变者Elver物品代码大全 Elver地图全ID代码汇总...
  18. Windows下PostgreSQL 8.1版安装图解
  19. 乳腺癌病理图像处理综述
  20. 在少儿编程中使用easygui来实现交互(1)——msgbox

热门文章

  1. Android Debug Bridge (ADB)
  2. 仙游一中,郑毓煌:“演义”人生
  3. 基于云计算智慧校园综合解决方案
  4. 通过独立站打开新市场
  5. 扫普通链接二维码打开小程序
  6. 在一个项目上跑起来Docker(goland)
  7. project顶行显示项目名称
  8. python统计各省大学数_“双一流”大学各省分布情况统计
  9. 为何钓鱼网站一钓一个准?Python Django带你搭建steam钓鱼网站
  10. 煤都也要迈向互联网 抚顺将打造东北亚一流的电商产业基地