在ArcGIS中应用脚本实现批处理

Zhuotong Nan(南卓铜) [email protected]

ArcGIS 9.2提供了脚本功能,可以方便地实现批处理功能。可用脚本包括JS, VBS和Python。以下示例利用Python实现批处理ASCII grid文件到ESRI Binary grid转换的功能。

问题

ASCII grid由matlab代码生成,存储于 LDAS.data 目录。ASCII grid内容如下:

ncols         32
nrows         32
xllcorner     -88
yllcorner     37.75
cellsize      0.125
NODATA_value  -9999
0.000000    0.000000 …

各ASCII grids命名有以下规律:

nbwi3.[yyyy].[mm].[dd].[hh]h.txt

其中[yyyy]是4位年份,[mm]是月份,[dd]是2位日期,[hh]是24制的小时。比如,nbwi3.2001.07.08.15h.txt等。

生成的ESRI Binary grid有以下命名规律:n[yy][mm][dd][hh],其中[yy]是2位年份,[mm]是月份,[dd]是2位日期,[hh]是24制的小时。如n01070814。注意,ESRI grid的命名和长度是有一定要求限制的,所以我们必须要选择合适的,并能区别数据的命名方式。

我们需要做的是将数百个ASCII grids转换成对应的ESRI binary grids。

实现

脚本编写环境我们这里采用免费的PyDev/Eclipse。Python脚本存储在与LDAS.data并列的src目录下。事实上,可以使用任何文本编辑软件来用脚本,但Eclipse提供很多编程增强功能,如智能提示等。

import arcgisscripting
import os

datadir=os.path.abspath(os.path.join(os.getcwd(),"../LDAS.data"))
#print datadir
children=os.listdir(datadir)

#only ".txt" files
txtfiles=[]
grdnames=[]
for child in children:
    root,ext=os.path.splitext(child)
    if (ext==’.txt’):
        txtfiles.append(child);
        grdname=’n’+root[8:19]
        grdname=grdname.replace(‘.’,”)
        grdnames.append(grdname)
# grid names now ready       

gp=arcgisscripting.create()
gp.workspace=datadir

# begin iteration
for i in range(len(grdnames)):
    gp.ASCIIToRaster(txtfiles[i], grdnames[i], "FLOAT")
    print txtfiles[i]

首先,需要通过import arcgisscripting,将arcgisscripting导入Python。arcgisscripting事实上是位于C:Program Filesarcgisbin目录下的arcgisscripting.dll。如果在运行时,提示找不到arcgisscripting库,则需要配置Python库路径。

库导入成功后,gp=arcgisscripting.create() 创建script对象,此后通过“gp.命令”的方式调用script支持的命令。一般来讲,可以在arcgis command window里使用的命令,都会有对象的script调用方式。比如,

ASCIIToRaster的命令语法是ASCIIToRaster_conversion <in_ascii_file> <out_raster> {INTEGER | FLOAT},对应的脚本语法是ASCIIToRaster_conversion (in_ascii_file, out_raster, data_type)。我们可以在ArcGIS Desktop帮助文档里找到。

此外,gp还有一些专用的script命令,比如ListRasters、GetMessage等,大家可以在ArcGIS Desktop帮助文档查询Geoprocessor object。这些命令对于进行批处理迭代时十分有用。

在以上代码中,与geoprocessor有关的代码有,

gp=arcgisscripting.create()
gp.workspace=datadir
gp.ASCIIToRaster(txtfiles[i], grdnames[i], "FLOAT")

workspace设置了当前的工作目录,相当于在command window里执行 “workspace 目录”。

ASCIIToRaster执行实质的转换工作。如果此处我们需要执行位于ArcGIS扩展模块的命令,则需要在调用命令前,先对扩展模块(如spatial analysis)进行checkout。使用以下命令:

gp.CheckOutExtension("Spatial")
gp.CellStatistics_sa(…)

否则CellStatistic_sa会抱怨说没有License可用。

代码其余部分是常规Python代码。以下取得指定目录下的txt扩展名的全部文件名,并生成对应的grid文件名。

children=os.listdir(datadir)

#only ".txt" files
txtfiles=[]
grdnames=[]
for child in children:
    root,ext=os.path.splitext(child)
    if (ext==’.txt’):
        txtfiles.append(child);
        grdname=’n’+root[8:19]
        grdname=grdname.replace(‘.’,”)
        grdnames.append(grdname)

Python十分有趣,如果有其它语言基础,学习起来并不难。对我们写ArcGIS脚本,有一般的Python知识就足够了。尽管效率不如传统的arcinfo AML高,但在功能上则不逊色。如果需要实现十分复杂的GIS功能,则建议使用c++, vb或.net语言操作ArcObjects。

汉字数字化?

IPv9方兴未艾,据说得到信息部的支持。这里汉字数字化方案又浮出水面(其实是旧事
重提了)。据说四川代表(人大代表么?)提议,“建议尽快制定《汉字数字化方
案》”。
看到“现代计算机技术是以英文为基础设计的,中文被长期排斥在中央处理器之外”,
很是疑惑,排斥在CPU之外的岂止中文,任何人类语言只是某种外在表现,哪个学过计
算机的不知道计算机语言其实是一系列的0和1(电脉冲)。
然而,果不其然,又看到这个所谓的“汉字数字化”功能多多,不仅是输入法,还解决
加密,人工智能等诸多难题,与“十进制网络”如出一辙。
除了笑笑还能说什么。
突然又想到以前曾经有个汉字编程也风靡一时,据说还拿了不少的研究经费,不知道现
在进展如何了。
查看原新闻

最近研究一下了HDF

因为需要对modis数据进行处理,研究了一下hdf和hdf eos两个类库
hdf是个好东西,不过有点太复杂,而且api尽管很强大,但工具还是相对不多,可以windows下使用的则更少
 

珍惜

不同的人的生活轨迹,就是在坐标系里的每条游走的曲线,在沿着时间轴不停的发展变化,然后有一些曲线就在某些阶段会相交到一起,有些会重合走一段时间,也许以后会分开。但65亿条曲线,能走到一起的,能有多少。缘份是值得珍惜的。

刘东生院士过世了

看到一个小块新闻,但对我们在这领域工作的人来讲比“陈冠希又出了几张艳照”可来得震憾的多。新闻说黄土学家刘东生院士过世了。以前在北京开会见过刘院士做过报告。十分了不起的一个人,作出了很优秀的工作成果,尤其这些年很高龄了还是很敬业,经常能看到他的身影,精神值得我们年轻人学习。他是国内地学界真正了不起的科学家之一,获过Tylor环境奖和国家自然科学最高奖。这两个奖的重量每个搞地学的人都知道,基本等同于最高荣誉了。

Win XP sp2远程桌面并发登陆补丁

sp2最终版本只支持同时一个在使用远程桌面,很不方便。以下方法可以使得3用户同时使用。

1. 下载这里的termserv.zip,解压缩

2. 在safe模式下启动机器,将c:windowssystem32termsrv.dll改名为 termsrv_sp2.dll,将这里的termsrv.dll拷贝到c:windowssystem32下。这里提供的termsrv.dll是sp2第一版本带的,所以足够安全。

3. 运行带的Concurrent Remote Sessions SP2.reg,对注册表打补丁

4. 需要打开“快速用户切换”

5. 打开组策略编辑器,开始菜单,运行,gpedit.msc,到Computer Configuration > Administrative Templates > Windows Components > Terminal Services这里,将Limit Number of Connection设为enable,并设为3(多了没用)。

6. 重启机器。现在你可以登陆3个用户了,比如一个控制台,2个远程(远程需为不同用户名)。

我在台式机上试了,好用。而且应该不涉及版权,反正不会很严重。

下载补丁包

http://sig9.com/node/110

卸载网筝NK800 v2

南卓铜([email protected])

西部数据中心目前提供的网筝驱动程序v2(20060927)安装后不能正常卸载。我们已经向软件提供商反映此问题。这些提供了一个临时的解决方案。

1. 在开始菜单,找到“卸载网筝NK-800”,点击“属性”,copy目标里的内容。

image

2. 找到从westdc上下载来的驱动文件,如果是压缩包,请解压缩到一个目录,比如c:tempnk800目录下。这个路径以下有用。

3. 打开command窗口(开始->运行,键入cmd),将步骤1里目标内容,粘贴到命令行下(右键,粘贴),如下:

image

移动鼠标,更改

“C:Program FilesIn
stallShield Installation Information{9147E0F5-074A-4D73-B8EA-63FDE7C70330}Setu
p.exe” (注意,{}内的可能不一样)

为步骤2找到的位置(该目录下应该有原始的Setup.exe等文件)。我这里是,

“c:tempnk800Setup.exe”

现在完整命令如下所示,

C:WINDOWSsystem32RunDll32.exe C:PROGRA
~1COMMON~1INSTAL~1engine6INTEL3~1Ctor.dll,LaunchSetup “c:tempnk800Setu
p.exe”  -uninst  -f

4. 运行。按照出现的图形界面提示,完成卸载。

Enable delayed content loading for Cuyahoga Module

南卓铜([email protected])

Cuyahoga is a powerful and well designed Web-based Content Management System. In current version, however, there is not yet Ajax supported. This brief article will show you how we can implement an Ajax based delayed content loading for its module.

The project I am doing will achieve Asian water news from an external website, Global Water News. To do this, I developed a set of classes to download content from external page and then parse them. However, in a synchronous way, the content downloading and parsing process is rather slow, consequently affecting the display of our homepage.  A delayed loading of this part of content will be ideal for circumventing this problem. 

Figure below shows the schema we developed for Cuyahoga framework.

See www.asian-gwadi.org homepage for online demonstration.

image

1. Enable Ajax support

Download latest ASP.net Ajax toolkit for framework 2.0 and install it. Add reference to System.Web.Extensions in Web project.

Open a brand new Ajax based web project. This will generate necessary configuration for Ajax support in a Web project.  Merge web.config created in this new project to Cuyahoga’s root web.config.

2. Add a Cuyahoga module for your purpose

image

AsianWaterNewsModule is just derived on ModuleBase and simple leave everything blank.

AsianWaterNewsControl.ascx is the user control working together with the module. It’s based on BaseModuleControl. It simply defines the container where we will populate data later.

AsianWaterNewsControl_Under.ascx is the underlying user control who download data from external web page, parse them and then fill up a repeater control.

AsianWaterNewsFristPage.cs, WaterItem.cs, and WaterNewsPageContent.cs are business logic codes to help AsianWaterNewsContol_Under control to realize its functionality.

AsianWaterNewsService.asmx define a web service, which renders the underlying user control, AsianWaterNewsControl_Under.ascx, to HTML codes. The output of this web services is expected to populate the placeholder contained in Cuyahoga user control, i.e., AsianWaterNewsControl.ascx.

Default.js and jquery-1.2.2.min.js are essential callback javascripts to be called in client side.

Install directory contains sql scripts for database structure.

3. Procedure

When a module is loaded by Cuyahoga framework, user controls derived upon BaseModuleControl (here is AsianWaterNewsControl.ascx ) will be asked to display themselves. In AsianWaterNewsControl.ascx , Ajax script manager was instantiated to wrap webservices as well as to register necessary javascripts. Javascripts were then downloaded to client together with other HTML. The page were displayed.  At the same time, callback javascripts were activated to call web service (here AsianWaterNewsService.asmx), which returns HTML representation of underlying user control (here, AsianWaterNewsControl_Under.ascx) actually retrieving data from external web pages. Then a div placeholder in  AsianWaterNewsControl.ascx will be populated with data retrieved.

See my previous post to get understand how to realize delayed content loading. For English original article, see here.

Download source codes of the AsianWaterNewsModule project.

Note, CSS and images should be defined in the template currently applied to your site. I did not include those stuff in the source code package.

See http://www.asian-gwadi.org/ for the operational demo.

Web内容滞后装载的另一可行方法

原文在 http://www.codeproject.com/KB/ajax/DelayedContentLoading.aspx
大概思路是:在user control里实现ajax updatepanel和timer。首先下载到客户端的是user control的装载状态view,下载完后,自动启动timer的ontick,通过updatepanel调用user control的另一view正式装载数据。由于采用ajax所以可以实现页面的部分更新。
这方法也是可以考虑的。

最近几天

1. 周六书亚载了我们一家三口去教会,我是去看看,而且因为那是中国人聚集的地方,会很热闹。教会是在传福音。人都十分nice。我看样子是他们盯上的目标之一,使劲劝说我加入。看了洗礼的过程。领会了书亚的演讲才能,很棒。

2. 回来在Giant Eagle买了东西,回来朵朵说这次的葡萄比上次好看。我们也这样感觉,哈哈。结果一晚上我们一家吃掉了一大半。小赵总结说,橙子costo的最好吃,葡萄Giant Eagle最好,苹果则是Walmart不错。哦,基本上我们一家评价东西好差的时候,是不看价格的。

3. 周日本说是到经国家去,考虑到老是过去吃他花他,不好意思,另朵朵也不大喜欢出去,且有点发烧的样子。我睡了很长时间,一周下来感觉很累。看了好多集的Friends。听力是比以前好了一点,但也进步不是很明显。不过应付正常的生活问题不是很大了。

4. 今天醒来居然已经9:30,大惊,窗帘一拉开,外面已经大亮。闹钟又被朵朵给调了。赶到办公室,收邮件。看到wlx说国内服务器出问题了,发现黑客入侵。基本上花了我整一天时间(被老梁如果知道,又要被骂,但没法子,那也是我应该做的事),理了一下,将入侵的整个过程大致还原了一下。写所里同事领导写信,说明了整个过程。清除的事还是交给亮子了,需要更多的时间耐心细致的去做。听小赵说今天朵朵在幼儿园表现不错,能跟小朋友玩到一起了。