diff --git a/esri2open.tbx b/esri2open.tbx index 7738e06..fe24680 100644 Binary files a/esri2open.tbx and b/esri2open.tbx differ diff --git a/esri2open/esri2open.py b/esri2open/esri2open.py index 222c223..f65c5d8 100644 --- a/esri2open/esri2open.py +++ b/esri2open/esri2open.py @@ -13,11 +13,12 @@ # --------------------------------------------------------------------------- #imports from arcpy import AddMessage, GetCount_management -from utilities import getExt,listFields,getShp, parseProp +from utilities import getExt, listFields,getShp, parseProp, parseFieldType from parseRow import parse from csv import DictWriter from json import dump - +from sqlite3 import Connection +from os.path import splitext, split #---- #prepare files @@ -30,7 +31,7 @@ def prepareCSV(outJSON,featureClass,fileType,includeGeometry): for field in fields: if (fields[field] != u'OID') and field.lower() not in ('shape_length','shape_area','shape.len','shape.length','shape_len','shape.area',shp.lower()): fieldNames.append(field) - if includeGeometry: + if includeGeometry!="none": fieldNames.append("geometry") outCSV=DictWriter(out,fieldNames,extrasaction='ignore') fieldObject = {} @@ -38,34 +39,76 @@ def prepareCSV(outJSON,featureClass,fileType,includeGeometry): fieldObject[fieldName]=fieldName outCSV.writerow(fieldObject) return [outCSV,out] - +def prepareSqlite(out,featureClass,fileType,includeGeometry): + [shp,shpType]=getShp(featureClass) + if shpType == "point": + gType = 1 + elif shpType == "multipoint": + gType = 4 + elif shpType == "polyline": + gType = 5 + elif shpType == "polygon": + gType = 6 + fields=listFields(featureClass) + fieldNames = [] + for field in fields: + if (fields[field] != u'OID') and field.lower() not in ('shape_length','shape_area','shape.len','shape.length','shape_len','shape.area',shp.lower()): + fieldNames.append(parseFieldType(field,fields[field])) + if includeGeometry: + fieldNames.append("GEOMETRY blob") + conn=Connection(out.name) + c=conn.cursor() + c.execute("""CREATE TABLE spatial_ref_sys ( + srid INTEGER UNIQUE, + auth_name TEXT, + auth_srid INTEGER, + srtext TEXT )""") + c.execute("insert into spatial_ref_sys(srid ,auth_name ,auth_srid ,srtext) values(?,?,?,?)",(4326, u'EPSG', 4326, u'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]')) + name = splitext(split(out.name)[1])[0] + c.execute("""CREATE TABLE geometry_columns ( + f_table_name TEXT, + f_geometry_column TEXT, + geometry_type INTEGER, + coord_dimension INTEGER, + srid INTEGER, + geometry_format TEXT )""") + c.execute("""insert into geometry_columns( f_table_name, f_geometry_column, geometry_type, coord_dimension, srid,geometry_format) values(?,?,?,?,?,?)""",(name,"GEOMETRY",gType,2,4326)) + c.execute("create table {0}({1})".format(name,", ".join(fieldNames))) + return [name,c,conn] def prepareGeoJSON(outJSON,*args): out = open(outJSON,"wb") out.write("""{"type":"FeatureCollection","features":[""") - return [out] + return out def prepareJSON(outJSON,*args): out = open(outJSON,"wb") out.write("""{"rows":[""") - return [out] + return out def prepareFile(outJSON,featureClass,fileType,includeGeometry): if fileType == "geojson": return prepareGeoJSON(outJSON,featureClass,fileType,includeGeometry) elif fileType == "csv": return prepareCSV(outJSON,featureClass,fileType,includeGeometry) - if fileType == "json": + elif fileType == "json": return prepareJSON(outJSON,featureClass,fileType,includeGeometry) + elif fileType == "sqlite": + return prepareSqlite(outJSON,featureClass,fileType,includeGeometry) else: return False #---- #close file #---- def closeJSON(out): - out[0].write("""]}""") - out[0].close() + out.write("""]}""") + out.close() return True - + +def closeSqlite(out): + out[2].commit() + out[1].close() + return True + def closeCSV(out): out[1].close() return True @@ -96,7 +139,7 @@ def writeFile(outArray,featureClass,fileType,includeGeometry, first=True): return parser.cleanUp(row) #this is the main entry point into the module -def toOpen(featureClass, outJSON, includeGeometry="true"): +def toOpen(featureClass, outJSON, includeGeometry="geojson"): #check the file type based on the extention fileType=getExt(outJSON) #some sanity checking @@ -107,8 +150,13 @@ def toOpen(featureClass, outJSON, includeGeometry="true"): elif not fileType: AddMessage("this filetype doesn't make sense") return - #to deal with esri giving "true" instead of True - includeGeometry = (includeGeometry=="true") + #geojson needs geometry + if fileType=="geojson": + includeGeometry="geojson" + elif fileType=="sqlite": + includeGeometry="well know binary" + else: + includeGeometry=includeGeometry.lower() #open up the file outFile=prepareFile(outJSON,featureClass,fileType,includeGeometry) #outFile will be false if the format isn't defined diff --git a/esri2open/parseGeometry.py b/esri2open/parseGeometry.py index dd62b5f..f6b7896 100644 --- a/esri2open/parseGeometry.py +++ b/esri2open/parseGeometry.py @@ -1,12 +1,17 @@ +from wkt import getWKTFunc +from wkb import getWKBFunc + +def getPoint(pt): + return [pt.X,pt.Y] def parseLineGeom(line): out=[] lineCount=line.count if lineCount ==1: - return ["Point",[line[0].X,line[0].Y]] + return ["Point",getPoint(line[0])] i=0 while i