# -*- 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. linux 清除驱动对网卡的记录

    1.情况说明: 通过U盘做了一个linux 系统镜像(可读可写),设置eth0的ip=10.0.73.11.第一次启动的时候ip正确. 当我将该镜像)在另一台服务器启动的时候,无法通过10.0.73. ...

  2. [LintCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  3. ArcGIS Server 10.1 错误 service failed to start,

    启动发布的地图服务时出现如下错误: ERROR: service failed to start, ServiceStarter thread timeout. 具体原因未知. Google中说了可能 ...

  4. respond.min.js IE失效问题

    respond.min.js  用于解决 ie6-ie8媒体查询 兼容问题: 1. 需把文件置于服务器上,iis下运行 2.包含媒体查询的 css文件需 采用外链形式 3.头部引用的respond.m ...

  5. Java面试题大全(一)

    JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...

  6. 需要注意学习.net过程的要点

    基础部分 C# 基础语法 OOP的概念,面向对象的理解 继承 封装 多态 ASP.NET MVC (Web Form 用的越来越少,如果你不熟悉,可以不看) JavaScript 基础语法 如何在HT ...

  7. jsp&Sevelet基础详解

    1.用scriptlet标签在jsp中嵌入java代码: (1).<%!...%>可以在里面定义全局变量,方法,类,一般写在<head>内 (2).<%%>定义的是 ...

  8. openstack组件之keystone

    一 什么是keystone keystone是 OpenStack Identity Service 的项目名称.它在整个体系中充当一个授权者的角色. Keystone项目的主要目的是给整个opens ...

  9. Linux学习笔记---用户管理---组group

    组管理: (1)/etc/group 格式: 组名:密码:GID:组员

  10. Google Chrome浏览器中如何使用命令

    Google Chrome浏览器中如何使用命令 | 浏览:2974 | 更新:2014-02-23 23:12 | 标签:chrome 1 2 3 分步阅读 Google Chrome浏览器有很多的特 ...