GeoMason 基础教程1 --- 简介和读写GIS数据

GeoMason 是基于Mason的一个离散多Agent仿真库,支持使用真实的GIS数据。

我把例子也上传到资源里了:

http://download.csdn.net/download/u011680118/10227824


本教程中的内容参考自英文版的 GeoMason Cookbook,可以在GeoMason官网上下载到。

第一部分读写地理空间数据的英文目录如下:



一、 读Shape文件

// WIDTH and HEIGHT correspond to arbitrary display dimensions
private final int WIDTH = 300;
private final int HEIGHT = 300;
GeomVectorField vectorField = new GeomVectorField(WIDTH, HEIGHT);
try {
ShapeFileImporter.read("file:foo.shp", vectorField);
} catch (FileNotFoundException ex)
{ /* handle exception */ }

创建一个 GeomVectorField,并且用ShapeFileImporter.read()方法来读入Shape文件的数据。

PS:
1. 通常上述代码段可以位于SimState的子类构造器中或者位于start()方法中(每次load都会重新读入文件)
2. 加载进来的向量层(vector layer)的单位跟底层坐标系的单位一致,并且默认的点都是用笛卡尔坐标系表示的,加载大量的表面时可能有误差。
3. WIDTH, HEIGHT是跟Display对应的,有时可以不指定。


二、 读多个向量层

GeomVectorField firstVectorField = new GeomVectorField(WIDTH,HEIGHT);
GeomVectorField secondVectorField = new GeomVectorField(WIDTH,HEIGHT);
GeomVectorField thirdVectorField = new GeomVectorField(WIDTH,HEIGHT);
try {
ShapeFileImporter.read("file:foo.shp", firstVectorField);
ShapeFileImporter.read("file:bar.shp", secondVectorField);
ShapeFileImporter.read("file:baz.shp", thirdVectorField);
} catch (FileNotFoundException ex)
{ /* handle exception */ }
Envelope globalMBR = firstVectorField.getMBR();
globalMBR.expandToInclude(secondVectorField.getMBR());
globalMBR.expandToInclude(thirdVectorField.getMBR());
firstVectorField.setMBR(globalMBR);
secondVectorField.setMBR(globalMBR);
thirdVectorField.setMBR(globalMBR);

MBR --- 最小边界矩形,保证多层在展示时对齐
PS:
1. 在导入数据前,需要保证多个文件中的数据使用统一参考系。
2. 上述代码段同样可以位于SimState子类构造器或者start()中。


三、读Shape文件并取出部分属性值

GeomVectorField vectorField = new GeomVectorField(WIDTH,HEIGHT);
Bag desiredAttributes = new Bag();
desiredAttributes.add("NAME");
desiredAttributes.add("TYPE");
try {
ShapeFileImporter.read("file:foo.shp", vectorField, desiredAttributes);
} catch (FileNotFoundException ex)
{ /* handle exception */ }

每一个空间对象都被wrap在Mason Geometry对象中,我们可以取出对应属性的值。

Bag geometries = vectorField.getGeometries();
for (int i = 0; i < geometries.size(); i++)
{
MasonGeometry geometry = (MasonGeometry) geometries.objs[i];
int type = geometry.getIntegerAttribute("TYPE");
String name = geometry.getStringAttribute("NAME");
}

PS:

1. 你需要提前知道Shape文件中有哪些属性和值的类型。


四、 读入ARC/Info ASCII Grid File

GeomGridField gridField = new GeomGridField();
InputStream inputStream = new FileInputStream("foo.asc");
ArcInfoASCGridImporter.read(inputStream, GridDataType.INTEGER, gridField);

PS:

1. 需要提前知道file中数据的精度,并在read方法中指定


五、 读Grid和Vector混合数据

GeomVectorField vectorField = new GeomVectorField(WIDTH,HEIGHT);
GeomGridField gridField = new GeomGridField();
try {
ShapeFileImporter.read("file:vector.shp", firstVectorField);
InputStream inputStream = new FileInputStream("grid.asc");
ArcInfoASCGridImporter.read(inputStream, GridDataType.INTEGER, gridField);
} catch (FileNotFoundException ex)
{ /* handle exception */ }
Envelope globalMBR = vectorField.getMBR();
globalMBR.expandToInclude(gridField.getMBR());
vectorField.setMBR(globalMBR);
gridField.setMBR(globalMBR);

PS:

1. 需要两种数据的参考系一致,并且要指定Grid数据的单位,且要设置MBR。



六、 写Shape文件

ShapeFileExporter.write("foo", vectorField);

用于需要保存仿真状态的场景。


七、 写ARC/Info ASCII Grid file

try {
BufferedWriter writer = new BufferedWriter( new FileWriter("foo.asc") );
ArcInfoASCGridExporter.write(gridField, writer);
writer.close();
} catch (IOException ex) {
/* handle exception */
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anyanyamy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值