EXCEL、CSV、JSON数据进行地图发布

--------------------------

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# FeatureServicize.py
# Created on: 2016-11-12 09:11:52.00000
# Creater: 
# Usage: FeatureServicize <jobId> <fileType> <filePath> <options> <statusServiceURL> <result>
# Description:
# ---------------------------------------------------------------------------

# Set the necessary product code
# import arceditor

# Import arcpy module
import sys, os, arcpy
import httplib, urllib, urllib2
import urlparse
import json
import traceback
import tarfile
import shutil
import xlrd
import json
import csv
import zipfile
import codecs
import shapefile

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Set environment settings
arcpy.env.overwriteOutput = True

# Script arguments
jobId = arcpy.GetParameterAsText(0)
if jobId == '' or not jobId:
    jobId = '20181113120000000'

fileType = arcpy.GetParameterAsText(1)
if fileType == '' or not fileType:
    fileType = "xls"  # shp, xls, csv

filePath = arcpy.GetParameterAsText(2)
if filePath == '' or not filePath:
    filePath = ''

options = arcpy.GetParameterAsText(3)
if options == '' or not options:
    options = '{}'

statusServiceURL = arcpy.GetParameterAsText(4)
if statusServiceURL == '#' or not statusServiceURL:
    statusServiceURL = 'http://192.168.1.100:8080'  # 'http://192.168.1.100:8080/DB/statusReport'
    # statusServiceURL = 'http'

fileType = 'xls'
filePath = "D:/spatialData/Excel/testPointExtend.xlsx"  # testPointExtend.xlsx  # testPointOther.csv
options = '''{
    "sheetName": "sheet",
    "xField": "X",
    "yField": "Y"
}'''


'''
jsonFilePath = arcpy.env.scratchFolder + '/' + 'temp.json'
shapefilePath = '%scratchFolder%' + '/' + 'result.shp'
unzipPath = arcpy.env.scratchFolder
'''


shapefilePath = 'D:/spatialData/scratch/result.shp'
jsonFilePath = 'D:/spatialData/scratch/temp.json'
shapefilePath = 'D:/spatialData/scratch/result.shp'
unzipPath = 'D:/spatialData/scratch'


# Report Status
def ReportStatus(url, info={}):
    if len(url) > 0 and url:
        params = json.dumps(info)
        arcpy.AddMessage(params)
        headers = {"Content-type": "application/json; charset=utf-8", "Accept": "text/plain"}
        try:
            req = urllib2.Request(url, params, headers)
            response = urllib2.urlopen(req)
            if response.getcode() == 200:
                data = response.read()
                response.close()
        except Exception,e:
            arcpy.AddMessage("error info: {0}".format(traceback.format_exc()))
    return

# Download File
def DownloadFile(filePath):
    filePathResult = None
    if filePath.startswith('http'):
        filePathResult = arcpy.env.scratchFolder + '/' + 'download.tmp'
        try:
            urllib.urlretrieve(filePath, filePathResult)
            ReportStatus(statusServiceURL, {'jobId':jobId, 'status':'info', 'message':u"已完成数据下载!".encode('utf8')})
        except Exception,e:
            errorInfo = traceback.format_exc() # str(e.message)
            arcpy.AddError("error info: {0}".format(errorInfo))
            ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':u"数据下载失败,下载地址为:".encode('utf8')+filePath, 'errorInfo': errorInfo})
    elif os.path.exists(filePath):
        filePathResult = filePath
    else:
        errorInfo = u"数据文件不存在,文件地址为:".encode('utf8')+filePath
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':errorInfo, 'errorInfo': errorInfo})
    return filePathResult

# Transform Excel To Json
def ExcelToJson(excelPath, options):
    sheetName = ''
    xField = 'X'
    yField = 'Y'
    if "sheet" in options:
        sheetName = options['sheet']
    if "xField" in options:
        xField = options['xField']
    if "yField" in options:
        yField = options['yField']

    featureSet = {
        "displayFieldName" : "",
        "geometryType" : "esriGeometryPoint",
        "spatialReference" : {
            "wkid" : 4326,
            "latestWkid" : 4326
        }
    }

    try:
        with xlrd.open_workbook(excelPath) as excelFile:
            if excelFile.nsheets > 0:
                if sheetName == '' or not sheetName:
                    sheet = excelFile.sheet_by_index(0)
                else:
                    sheet = excelFile.sheet_by_name(sheetName)
                rowCount = sheet.nrows
                columnCount = sheet.ncols
                fieldAliases = {}
                fields = []
                features = []
                if rowCount > 1:
                    fieldNames = sheet.row_values(0)
                    fieldTypes = sheet.row_types(1)
                    print(fieldNames)
                    print(fieldTypes)
                    #
                    for i in range(0, columnCount):
                        fieldName = fieldNames[i]
                        fieldType = fieldTypes[i]
                        fieldAliases[fieldName] = fieldName
                        field = None
                        if fieldType == 1: # String
                            field = {
                                "name" : fieldName,
                                "type" : "esriFieldTypeString",
                                "alias" : fieldName,
                                "length" : 255
                            }
                        elif fieldType == 2: # Double
                            field = {
                                "name" : fieldName,
                                "type" : "esriFieldTypeDouble",
                                "alias" : fieldName
                            }
                        elif fieldType == 3: # Date
                            field = {
                                "name" : fieldName,
                                "type" : "esriFieldTypeString",
                                "alias" : fieldName,
                                "length" : 30
                            }
                        elif fieldType == 4: # Boolean
                            field = {
                                "name" : fieldName,
                                "type" : "esriFieldTypeString",
                                "alias" : fieldName,
                                "length" : 10
                            }
                        fields.append(field)
                    if xField not in fieldAliases:
                        raise Exception,"Invalid xField!"
                    if yField not in fieldAliases:
                        raise Exception,"Invalid yField!"
                    featureSet['fieldAliases'] = fieldAliases
                    featureSet['fields'] = fields

                    for i in range(1, rowCount):
                        #
                        feature = {}
                        attributes = {}
                        geometry = {}
                        for j in range(0, columnCount):
                            fieldName = fieldNames[j]
                            field = fields[j]
                            cellInfo = sheet.cell(i, j)
                            if field['type'] == "esriFieldTypeString":
                                attributes[fieldName] = cellInfo.value
                            else:
                                attributes[fieldName] = cellInfo.value
                            if fieldName == xField:
                                geometry['x'] = cellInfo.value
                            if fieldName == yField:
                                geometry['y'] = cellInfo.value
                            feature['attributes'] = attributes
                            feature['geometry'] = geometry
                        features.append(feature)
                    featureSet['features'] = features
                # print(featureSet)
                else:
                    raise Exception,"Invalid Row!"
            else:
                raise Exception,"Invalid Sheet!"
    except Exception,e:
        errorInfo = traceback.format_exc() # str(e.message)
        arcpy.AddError("error info: {0}".format(errorInfo))
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':u"Excel转换失败!".encode('utf8'), 'errorInfo': errorInfo})
    return featureSet

# Transform Excel To Shapefile
def ExcelToShapefile(excelPath, options):
    sheetName = ''
    xField = 'X'
    yField = 'Y'
    if "sheet" in options:
        sheetName = options['sheet']
    if "xField" in options:
        xField = options['xField']
    if "yField" in options:
        yField = options['yField']

    projectStr = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
    shpWriter = None

    try:
        filePathWithout = shapefilePath[0: len(shapefilePath)-4]
        shpWriter = shapefile.Writer(target=filePathWithout, shapeType=shapefile.POINT)
        with xlrd.open_workbook(excelPath) as excelFile:
            if excelFile.nsheets > 0:
                if sheetName == '' or not sheetName:
                    sheet = excelFile.sheet_by_index(0)
                else:
                    sheet = excelFile.sheet_by_name(sheetName)
                rowCount = sheet.nrows
                columnCount = sheet.ncols
                fieldAliases = {}
                if rowCount > 1:
                    fieldNames = sheet.row_values(0)
                    fieldTypes = sheet.row_types(1)
                    #
                    xIndex = -1
                    yIndex = -1
                    #
                    for i in range(0, columnCount):
                        fieldName = fieldNames[i]
                        fieldType = fieldTypes[i]

                        if fieldType == 1: # String
                            shpWriter.field(fieldName, 'C', size=255)
                        elif fieldType == 2: # Double
                            shpWriter.field(fieldName, 'F', decimal=10)
                        elif fieldType == 3: # Date
                            shpWriter.field(fieldName, 'C', size=255)
                        elif fieldType == 4: # Boolean
                            shpWriter.field(fieldName, 'C', size=255)
                        if fieldName == xField:
                            xIndex = i
                        if fieldName == yField:
                            yIndex = i
                    if xIndex == -1:
                        raise Exception,"Invalid xField!"
                    if yIndex == -1:
                        raise Exception,"Invalid yField!"

                    for i in range(1, rowCount):
                        values = []
                        shpWriter.point(sheet.cell(i, xIndex).value, sheet.cell(i, yIndex).value)
                        for j in range(0, columnCount):
                            fieldType = fieldTypes[j]
                            if fieldType != 2 and fieldType != 1:
                                values.append(str(sheet.cell(i, j).value))
                            else:

                                values.append(sheet.cell(i, j).value)
                        shpWriter.record(*values)
                    fileProjectPath = filePathWithout + '.prj'
                    with open(fileProjectPath,'w') as fileProject:
                        fileProject.write(projectStr)
                # print(featureSet)
                else:
                    raise Exception,"Invalid Row!"
            else:
                raise Exception,"Invalid Sheet!"
    except Exception,e:
        errorInfo = traceback.format_exc() # str(e.message)
        arcpy.AddError("error info: {0}".format(errorInfo))
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':u"Excel转换失败!".encode('utf8'), 'errorInfo': errorInfo})
    finally:
        if shpWriter != None:
            shpWriter.close()

# Transform CSV To Json
def CsvToJson(csvPath, options):
    delimiter = ','
    xField = 'X'
    yField = 'Y'
    if "delimiter" in options:
        delimiter = options['delimiter']
    if "xField" in options:
        xField = options['xField']
    if "yField" in options:
        yField = options['yField']

    featureSet = {
        "displayFieldName" : "",
        "geometryType" : "esriGeometryPoint",
        "spatialReference" : {
            "wkid" : 4326,
            "latestWkid" : 4326
        }
    }
    try:
        with open(csvPath,'r') as csvfile:
            reader  = csv.reader(csvfile, delimiter = delimiter)
            head_row = next(reader)
            if head_row == '' or head_row == None:
                raise Exception,"Invalid CSV!"
            fieldAliases = {}
            fields = []
            features = []
            xIndex = -1
            yIndex = -1
            columnCount =len(head_row)
            fieldNames = []
            for i in range(0, columnCount):
                fieldName = head_row[i]
                fieldAliases[fieldName] = fieldName
                field = {
                    "name" : fieldName,
                    "type" : "esriFieldTypeString",
                    "alias" : fieldName,
                    "length" : 255
                }
                fields.append(field)
                featureSet['fieldAliases'] = fieldAliases
                featureSet['fields'] = fields
                if fieldName == xField:
                    xIndex = i
                if fieldName == yField:
                    yIndex = i
                fieldNames.append(fieldName)
            if xIndex == -1:
                raise Exception,"Invalid xField!"
            if yIndex == -1:
                raise Exception,"Invalid yField!"
            for row in reader:
                #
                feature = {}
                attributes = {}
                geometry = {}
                for j in range(0, columnCount):
                    attributes[fieldNames[j]] = row[j]
                    if j == xIndex:
                        geometry['x'] = float(row[j])
                    if j == yIndex:
                        geometry['y'] = float(row[j])
                    feature['attributes'] = attributes
                    feature['geometry'] = geometry
                features.append(feature)
                #
            featureSet['features'] = features
            # print(featureSet)
    except Exception,e:
        errorInfo = traceback.format_exc() # str(e.message)
        arcpy.AddError("error info: {0}".format(errorInfo))
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':u"CSV转换失败!".encode('utf8'), 'errorInfo': errorInfo})
    return featureSet

# Transform CSV To Shapefile
def CsvToShapefile(csvPath, options):
    delimiter = ','
    xField = 'X'
    yField = 'Y'
    if "delimiter" in options:
        delimiter = options['delimiter']
    if "xField" in options:
        xField = options['xField']
    if "yField" in options:
        yField = options['yField']
    projectStr = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
    shpWriter = None

    try:
        filePathWithout = shapefilePath[0: len(shapefilePath)-4]
        shpWriter = shapefile.Writer(target=filePathWithout, shapeType=shapefile.POINT)
        with open(csvPath,'r') as csvfile:
            reader  = csv.reader(csvfile, delimiter = delimiter)
            head_row = next(reader)
            if head_row == '' or head_row == None:
                raise Exception,"Invalid CSV!"
            xIndex = -1
            yIndex = -1
            columnCount =len(head_row)
            for i in range(0, columnCount):
                fieldName = head_row[i]
                if fieldName == xField:
                    xIndex = i
                if fieldName == yField:
                    yIndex = i
                shpWriter.field(fieldName, 'C', size=255)
            if xIndex == -1:
                raise Exception,"Invalid xField!"
            if yIndex == -1:
                raise Exception,"Invalid yField!"
            for row in reader:
                #
                values = []
                for j in range(0, columnCount):
                    values.append(row[j])
                shpWriter.point(float(row[xIndex]), float(row[yIndex]))
                shpWriter.record(*values)
        fileProjectPath = filePathWithout + '.prj'
        with open(fileProjectPath,'w') as fileProject:
            fileProject.write(projectStr)
    except Exception,e:
        errorInfo = traceback.format_exc() # str(e.message)
        arcpy.AddError("error info: {0}".format(errorInfo))
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':u"CSV转换失败!".encode('utf8'), 'errorInfo': errorInfo})
    finally:
        if shpWriter != None:
            shpWriter.close()

# Output JSON
def OutputJson(featureSet):
    jsonStr = json.dumps(featureSet) #  , ensure_ascii=False
    with codecs.open(jsonFilePath,'w') as jsonFile:  # 'utf-8'
        jsonFile.write(jsonStr)

# Transform Json To Shapefile
def TransformFormat():
    # Process: JSON To Features
    arcpy.JSONToFeatures_conversion(jsonFilePath, shapefilePath)
    ReportStatus(statusServiceURL, {'jobId':jobId, 'status':'info', 'message':u"已完成数据转换!".encode('utf8')})

# Unzip Shapefile
def UnzipShapefile(zipPath):
    global shapefilePath
    zip = None
    try:
        zip = zipfile.ZipFile(zipPath, 'r')
        zip.extractall(unzipPath)
        current_files = os.listdir(unzipPath)
        for file_name in current_files:
            if file_name.lower().endswith('.shp'):
                shapefilePath = unzipPath + '/' + file_name
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status':'info', 'message':u"已完成数据解压!".encode('utf8')})
    except Exception,e:
        errorInfo = traceback.format_exc()
        arcpy.AddError("error info: {0}".format(errorInfo))
        ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'error', 'message':u"数据解压失败!".encode('utf8'), 'errorInfo': errorInfo})
    finally:
        zip.close()

if __name__ == "__main__":
    #
    filePathInput = DownloadFile(filePath)
    optionsObj = json.loads(options)
    if fileType == 'xls':
        '''
        featureSet = ExcelToJson(filePathInput, optionsObj)
        OutputJson(featureSet)
        TransformFormat()
        '''
        ExcelToShapefile(filePathInput, optionsObj)
    elif fileType == 'csv':
        '''
        featureSet = CsvToJson(filePathInput, optionsObj)
        OutputJson(featureSet)
        TransformFormat()
        '''
        CsvToShapefile(filePathInput, optionsObj)
    elif fileType == 'shp':
        UnzipShapefile(filePathInput)
    arcpy.SetParameterAsText(5, shapefilePath)
    ReportStatus(statusServiceURL, {'jobId':jobId, 'status': 'finish', 'message':u"数据发布成功!".encode('utf8')})


-------------------------

上一篇:Qt根据文件后缀名过滤文件


下一篇:Qt删除目录下所有文件和文件夹