# geotools中泰森多边形的生成

1、定义

2、建立步骤

3、特征

1）每个泰森多边形内仅含有一个离散点数据； 2）泰森多边形内的点到相应离散点的距离最近； 3）位于泰森多边形边上的点到其两边的离散点的距离相等。

geotools中的生成

1、创建测试点

```	    int xmin = 0, xmax=180;
int ymin = 0, ymax=90;
Random random = new Random();
List<Geometry> geomsPoints = new ArrayList<Geometry>();
for(int i=0;i<100;i++){
int x = random.nextInt(xmax)%(xmax-xmin+1) + xmin,
y = random.nextInt(ymax)%(ymax-ymin+1) + ymin;
Coordinate coord = new Coordinate(x,y,i);
clipEnvelpoe.expandToInclude(coord);
}```

2、生成泰森多边形

```	    voronoiDiagramBuilder.setSites(coords);
voronoiDiagramBuilder.setClipEnvelope(clipEnvelpoe);
Geometry geom = voronoiDiagramBuilder.getDiagram(JTSFactoryFinder.getGeometryFactory());
List<Geometry> geoms = new ArrayList<Geometry>();
for(int i=0;i<geom.getNumGeometries();i++){
}```

```package com.lzugis.geotools;

import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.triangulate.VoronoiDiagramBuilder;

public class TsdbxTest {
static TsdbxTest tsdbx = new TsdbxTest();
public void voronoiTest(){
VoronoiDiagramBuilder voronoiDiagramBuilder = new VoronoiDiagramBuilder();
List<Coordinate> coords = new ArrayList<Coordinate>();
Envelope clipEnvelpoe = new Envelope();
int xmin = 0, xmax=180;
int ymin = 0, ymax=90;
Random random = new Random();
List<Geometry> geomsPoints = new ArrayList<Geometry>();
for(int i=0;i<100;i++){
int x = random.nextInt(xmax)%(xmax-xmin+1) + xmin,
y = random.nextInt(ymax)%(ymax-ymin+1) + ymin;
Coordinate coord = new Coordinate(x,y,i);
clipEnvelpoe.expandToInclude(coord);
}
String pointpath = "d:/data/tsdbxpt.shp";
tsdbx.writeShape(pointpath,"Point", geomsPoints);

voronoiDiagramBuilder.setSites(coords);
voronoiDiagramBuilder.setClipEnvelope(clipEnvelpoe);
Geometry geom = voronoiDiagramBuilder.getDiagram(JTSFactoryFinder.getGeometryFactory());
List<Geometry> geoms = new ArrayList<Geometry>();
for(int i=0;i<geom.getNumGeometries();i++){
}

String polygonpath = "d:/data/tsdbx.shp";
tsdbx.writeShape(polygonpath,"Polygon", geoms);
}
/**
*
* @param filepath
* @param geoType
* @param geoms
*/
public void writeShape(String filepath, String geoType, List<Geometry> geoms) {
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");
if(geoType=="Point"){
}
else{
}

ds.createSchema(tb.buildFeatureType());
//设置编码
Charset charset = Charset.forName("GBK");
ds.setCharset(charset);
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
for(int i=0,len=geoms.size();i<len;i++){
//写下一条
SimpleFeature feature = writer.next();
Geometry geom = geoms.get(i);
feature.setAttribute("the_geom", geom);
}
writer.write();
writer.close();
ds.dispose();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args){
long start = System.currentTimeMillis();
tsdbx.voronoiTest();
System.out.println("共耗时"+(System.currentTimeMillis() - start)+"ms");
}
}```

1、百度百科

261 篇文章53 人订阅

0 条评论

## 相关文章

3347

2513

2.2K8

2975

3669

5298

3437

20110

### 每日一练（2017/5/22）

Java基础 | 数据库 | Android | 学习视频 | 学习资料下载 课前导读 ●回复"每日一练"获取以前的题目！ ●答案公布时间：为每期发布题目的第二...

3428

1782