# -*- coding: utf-8 -*-
import struct
import decimal
import itertools
import arcpy
import math
import sys
import datetime
import cx_Oracle
import json
import os
import time
import uuid
import logging
from arcpy import env
from arcpy.sa import * #参数:文件路径,字段序列,字段类型序列,数据记录序列
def dbfwriter(f, fieldnames, fieldspecs, records):
""" Return a string suitable for writing directly to a binary dbf file. File f should be open for writing in a binary mode. Fieldnames should be no longer than ten characters and not include \x00.
Fieldspecs are in the form (type, size, deci) where
type is one of:
C for ascii character data
M for ascii character memo data (real memo fields not supported)
D for datetime objects
N for ints or decimal objects
L for logical values 'T', 'F', or '?'
size is the field width
deci is the number of decimal places in the provided decimal object
Records can be an iterable over the records (sequences of field values). """
# header info
ver = 3
now = datetime.datetime.now()
yr, mon, day = now.year - 1900, now.month, now.day
numrec = len(records)
numfields = len(fieldspecs)
lenheader = numfields * 32 + 33
lenrecord = sum(field[1] for field in fieldspecs) + 1
hdr = struct.pack('<BBBBLHH20x', ver, yr, mon, day, numrec, lenheader, lenrecord)
f.write(hdr) # field specs
for name, (typ, size, deci) in itertools.izip(fieldnames, fieldspecs):
name = name.ljust(11, '\x00')
fld = struct.pack('<11sc4xBB14x', name, typ, size, deci)
f.write(fld) # terminator
f.write('\r') # records
for record in records:
f.write(' ') # deletion flag
for (typ, size, deci), value in itertools.izip(fieldspecs, record):
if typ == "N":
value = str(value).rjust(size, ' ')
elif typ == 'D':
value = value.strftime('%Y%m%d')
elif typ == 'L':
value = str(value)[0].upper()
else:
value = str(value)[:size].ljust(size, ' ')
assert len(value) == size
f.write(value) # End of file
f.write('\x1A')
##################################################################### LOG_FILENAME="E:\update\log.txt";
t = datetime.datetime.now();
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG);
logging.debug(str(t)+"闪电数据处理开始:"); #修改默认系统编码格式,让系统支持中文
reload(sys);
#print sys.getdefaultencoding();
sys.setdefaultencoding('utf8'); env.workspace = r"e:\update";
logging.debug("1、从数据库查询数据,然后保存到dbtable.dbf中(计算值工具不能用于python编辑)");
cnxn = cx_Oracle.connect('ycybyj/ycybyj@127.0.0.1:1521/orcl');
cursor0 = cnxn.cursor();
#查询出字段用ID,SHI1,XIAN1 分析完后字段用SHI,XIAN,SHENG
querySql="select ID,LONGITUDE,LATITUDE,SHI SHI1,XIAN XIAN1 from lightning where sheng is not null and rownum<=100";
cursor0.execute(querySql);
records=[];
for item in cursor0:
records.append(item); #生成dbtable之前,需要先把现有的dbtable.dbf删除
filename ='e:/update/shp/dbtable.dbf'
if arcpy.Exists(filename):
try:
arcpy.Delete_management(filename, "")
except:
arcpy.AddError("无法删除dbtable.dbf:" + filename)
f = open(filename, 'wb+')
fieldnames=['ID','LONGITUDE','LATITUDE','SHI1','XIAN1'];
fieldspecs=[('N',10,0),('N',10,4),('N',10,4),('C',50,0),('C',50,0)];
dbfwriter(f, fieldnames, fieldspecs, records)
f.close() logging.debug("2、利用XY事件图层工具,把数据库表转换成要素类");
arcpy.MakeXYEventLayer_management(filename, "LONGITUDE", "LATITUDE", "out_layer");
nums=arcpy.GetCount_management("out_Layer");
logging.debug("2.1、 该次处理的数据行数是:"+str(nums)); logging.debug("3、利用空间链接工具,获取到闪电的行政区");
joinfc=r"e:\update\shp\xian.shp";
outfc=r"e:\update\shp\out.shp";
#生成out.shp之前,需要先把现有的out.shp删除
if arcpy.Exists(outfc):
try:
arcpy.Delete_management(outfc, "")
except:
arcpy.AddError("无法删除out.shp:" + outfc)
arcpy.SpatialJoin_analysis("out_layer", joinfc, outfc); logging.debug("4、将结果shp里的信息更新到数据库,唯一标识用ID");
cursor = cnxn.cursor();
cursor1 = arcpy.SearchCursor(outfc)
for row in cursor1:
ids=row.getValue("ID");
ids=int(ids);
xian=row.getValue("XIAN");
shi=row.getValue("SHI");
sheng=row.getValue("SHENG");
if sheng==' ':
sheng='非安徽省';
updatesql="update lightning set sheng='"+sheng+"', xian='"+xian+"',shi='"+shi+"' where id="+str(ids);
cursor.execute(updatesql);
cnxn.commit(); #删除不在行政区边界内的闪电点位
delSql="delete from lightning where sheng='非安徽省'";
cursor.execute(updatesql);
cnxn.commit(); #关闭系统资源
cursor0.close;
cursor.close;
cnxn.close; tt = datetime.datetime.now();
logging.debug(str(tt)+"闪电数据处理结束.");

数据库的点数据根据行政区shp来进行行政区处理,python定时器实现的更多相关文章

  1. [转载] SQL获取所有数据库名、表名、储存过程以及参数列表

    查询一个数据库中所有表字段属性的sql语句       1.获取所有用户名: SELECT name FROM Sysusers where status='2' and islogin='1' is ...

  2. SQL获取所有数据库名、表名、储存过程以及参数列表

    SQL获取所有数据库名.表名.储存过程以及参数列表 1.获取所有用户名:SELECT name FROM Sysusers where status='2' and islogin='1'islogi ...

  3. 读数据库所有表和表结构的sql语句

    SQL获取所有数据库名.表名.储存过程以及参数列表 1.获取所有用户名:SELECT name FROM Sysusers where status='2' and islogin='1'islogi ...

  4. Web中树形数据(层级关系数据)的实现—以行政区树为例

    在Web开发中常常遇到树形数据的操作,如菜单.组织机构.行政区(省.市.县)等具有层级关系的数据. 以下以行政区为例说明树形数据(层级关系数据)的存储以及实现,效果如图所看到的. 1 数据库表结构设计 ...

  5. Cesium之3D拉伸显示行政区

    转自原文 Cesium之3D拉伸显示行政区含GeoJSON数据生成过程GDAL的ogr2ogr Cesiumjs 是一套javascript库,用来渲染3D地球,2D区域地图,和多种GIS要素.不需要 ...

  6. Python操作Mysql数据库时SQL语句的格式问题

    一.概述 近日使用Python对Mysql数据库进行操作,遇到SQL语句死活出问题的情况.由于最初没有将异常打印出来,一直不知道原因.随后,将异常打印出来之后,通过异常信息,对代码进行修改.最终,成功 ...

  7. Python爬虫爬取豆瓣电影名称和链接,分别存入txt,excel和数据库

    前提条件是python操作excel和数据库的环境配置是完整的,这个需要在python中安装导入相关依赖包: 实现的具体代码如下: #!/usr/bin/python# -*- coding: utf ...

  8. AMap行政区查询服务

    AMap.DistrictSearch行政区查询服务插件,提供全国各省.市.县.区的中心点经纬度.行政区边界坐标组.下级行政区等信息.根据行政区边界坐标组可在地图上绘制行政区边界.(本文为原创,并在项 ...

  9. python3.4怎么连接mysql pymysql连接mysql数据库

    本文介绍了python3 4连接mysql数据库的方法,在python3 4中使用原来python2 7的mysqldb已不能连接mysql数据库了,可以使用pymysql.   在python3.4 ...

随机推荐

  1. android-ProgressBar

    制定ProgressBar显示风格 * 参考系统自带的进度条 * ProgressBar分类 * 可以精确显示进度(可以显示刻度和百分比) * 不可以精确显示进度 * 标题上ProgressBar的设 ...

  2. 无法在提交表单前通过ajax验证解决办法

    博主在一个小项目中,要实现注册表单无刷新验证用户名或密码,但是发现不管怎么样都无法在先通过ajax验证之前不提交表单. 例如:一个简单的验证函数 function check(){ $.post(&q ...

  3. POJ 3321 树状数组(+dfs+重新建树)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27092   Accepted: 8033 Descr ...

  4. thinkphp框架3.2的cookie删除问题记录

    在使用框架删除cookie时,发现cookie(null)不起作用.后来查看官网相关信息,看到了讨论http://www.thinkphp.cn/bug/2602.html

  5. JavaScript 几种简单的table切换

    方法一:for循环+if判断当前点击与自定义数组是否匹配 <!DOCTYPE html> <html lang="en"> <head> < ...

  6. (转)实例分析:MySQL优化经验

    [IT专家网独家]同时在线访问量继续增大,对于1G内存的服务器明显感觉到吃力,严重时甚至每天都会死机,或者时不时的服务器卡一下,这个问题曾经困扰了我半个多月.MySQL使用是很具伸缩性的算法,因此你通 ...

  7. php有效的过滤html标签,js代码,css样式标签

    过滤html标签�php中太简单了,我们可以直接使用strip_tags函数来实现了,下面给各位整理了一些关于 strip_tags函数的例子. php过滤html的函数:strip_tags(str ...

  8. JavaAnnotation和反射简化开发

    Part I 我并不是在卖弄自己的英语有多少的了不起,只不过对Annotation这一次的解释真的很懊恼,“注解”和“注释”这两个对Annotation的翻译我听着不爽,所以全文都用Annotatio ...

  9. php课程---语句及函数

    语句:    一:分支语句        1.if(条件1){满足条件1执行}    2.if(条件1){满足条件1执行}else{不满足条件1执行}    3.if(条件1){满足条件1执行}els ...

  10. 关于linux密码忘记问题解决方法

    最近在试装centos,不小心把密码丢了,进不去,查了一下资料,找到了方法,试验成功.存在这里,备用. 1. 在出现grub画面时,用上下键选中你平时启动linux的那一项,然后按e键 2. 再次用上 ...