def __init__(this, workspace ,convertType): arcpy.env.workspace =str(workspace.encode("cp1256")) workspacedesc = arcpy.Describe(arcpy.env.workspace) Database= str(workspace.encode("cp1256")).split('\\')[-1:][0] FileType =str(workspacedesc.workspaceType).encode("cp1256") workspaceEncoding= str(workspace.encode("cp1256")).decode("cp1256") filePath = str(os.path.dirname(workspaceEncoding.encode("cp1256"))).decode("cp1256") NewDbName = filePath + "\\" NewDbName = NewDbName + Database.decode("cp1256").rsplit( ".", 1 )[ 0 ] + strftime("%Y%m%d_%H%M%S") gdb = workspace extension = '.gpkg' if convertType == 'OGC Sqlite': extension = '.sqlite' NewDbName = NewDbName + extension elif convertType =='Geo Package': extension = '.gpkg' NewDbName = NewDbName + extension arcpy.CreateSQLiteDatabase_management(NewDbName, 'GEOPACKAGE') print(NewDbName) this.conn = sqlite3.connect(NewDbName) this.convertType = convertType this.NewDbName = NewDbName this.workspace = arcpy.env.workspace this.extension = extension this.conn = this.enable_spatial()
def to_sqlite(df, out_folder, db_name, table_name, spatial_type="ST_GEOMETRY", overwrite=True): """ Creates a new sqlite database and imports the dataframe data to that database. Inputs: df: spatial dataframe to export out_folder: Location of the SQLite database to be created db_name: Name of the SQLite database file. An extension of .sqlite will be automatically assigned if the spatial_type is ST_GEOMETRY or SPATIALITE. If the spatial_type is GEOPACKAGE, a .gpkg extension is automatically assigned. table_name: name of the exported spatial dataframe spatial_type: The spatial type to install with the new SQLite database. ST_GEOMETRY-Esri's spatial storage type. This is the default. SPATIALITE-SpatiaLite spatial storage type. GEOPACKAGE-OGC GeoPackage dataset. overwrite: True will erase data if it exists, false will throw error if the sqlite database exists. Output: sqlite Database path location plus the feature class. """ if HASARCPY: sqldb = os.path.join(out_folder, db_name) if os.path.isdir(out_folder) == False: raise ValueError("Save folder does not exist") if os.path.isfile(sqldb) and overwrite: os.remove(sqldb) db = arcpy.CreateSQLiteDatabase_management(out_database_name=sqldb, spatial_type=spatial_type)[0] elif os.path.isfile(sqldb) and overwrite == False: db = sqldb elif os.path.isfile(sqldb) == False: db = arcpy.CreateSQLiteDatabase_management(out_database_name=sqldb, spatial_type=spatial_type)[0] return to_featureclass(df=df, out_location=db, out_name=table_name) else: raise ImportError("arcpy is required to perform this operation") return
def __enter__(self): # 一時ディレクトリを作成 self.temp_dir = tempfile.mkdtemp() self.sqliteFile = os.path.join(self.temp_dir, 'calc.sqlite') self.sqlFile = os.path.join(self.temp_dir, 'exec.sql') arcpy.CreateSQLiteDatabase_management(self.sqliteFile, 'SPATIALITE') # with instance return self
def _create_output_workspace(self, output_path, **kwargs): if output_path.exists(): raise ValueError( "Cannot create GeoPackage workspace, it already exists.") # create parent directory if it doesn't exist output_path.parent.mkdir(parents=True, exist_ok=True) spatial_type = "GEOPACKAGE" if kwargs[ "version"] == None else "GEOPACKAGE_{}".format(kwargs["version"]) arcpy.CreateSQLiteDatabase_management(str(output_path), spatial_type)
arcpy.MultipartToSinglepart_management(gaul, gaul_singleparted) print("Gaul converted to single part") # Process: Repair Geometry arcpy.RepairGeometry_management(gaul_singleparted, "DELETE_NULL") print("Geometries repaired") # Process: Add Geometry Attributes arcpy.AddGeometryAttributes_management(gaul_singleparted, "AREA_GEODESIC", "", "SQUARE_KILOMETERS", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]") print("Geodetic area computed") # Process: Create SQLite Database if arcpy.Exists(gaul_for_bound_corr_gpkg): print(gaul_for_bound_corr_gpkg, " already exists.") else: arcpy.CreateSQLiteDatabase_management(gaul_for_bound_corr_gpkg, "GEOPACKAGE") # Process: Feature Class to Feature Class with threshold >1km2 arcpy.FeatureClassToFeatureClass_conversion(gaul_singleparted, gaul_for_bound_corr_gpkg, "gaul_singleparted", "\"AREA_GEO\" >=1", "id_object \"id_object\" true true false 4 Long 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,id_object,-1,-1;id_country \"id_country\" true true false 4 Long 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,id_country,-1,-1;name_iso31 \"name_iso31\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,name_iso31,-1,-1;sovereign_ \"sovereign_\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,sovereign_,-1,-1;sovereig_1 \"sovereig_1\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,sovereig_1,-1,-1;sovereig_2 \"sovereig_2\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,sovereig_2,-1,-1;iso3 \"iso3\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,iso3,-1,-1;iso2 \"iso2\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,iso2,-1,-1;un_m49 \"un_m49\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,un_m49,-1,-1;source \"source\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,source,-1,-1;status \"status\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,status,-1,-1;original_d \"original_d\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,original_d,-1,-1;original_n \"original_n\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,original_n,-1,-1;source_cod \"source_cod\" true true false 254 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,source_cod,-1,-1;sqkm \"sqkm\" true true false 8 Double 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,sqkm,-1,-1;Shape_Length \"Shape_Length\" true true true 8 Double 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,Shape_Length,-1,-1,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,Shape_length,-1,-1;Shape_Area \"Shape_Area\" true true true 8 Double 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,Shape_Area,-1,-1,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,Shape_area,-1,-1;ORIG_FID \"ORIG_FID\" true true false 0 Long 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,ORIG_FID,-1,-1;AREA_GEO \"AREA_GEO\" true true false 4 Double 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/gaul_singleparted,AREA_GEO,-1,-1", "") print("Features exported in Geopackage") print('-------------------------------------------------------') endtime=datetime.now() totaltime= endtime-firststarttime print(' ') print('PROCEDURE COMPLETED. Elapsed time: ', totaltime) print('Now execute in docker the script "/globes/USERS/GIACOMO/protconn/scripts/gis_proc/exec_simplify_gaul_bound_correction.sh" ') print(' ') sys.exit()
arcpy.Merge_management( "Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/WDPA_poly_relevant;Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/WDPA_point_relevant_with_joined_count_multiparts_buffered", WDPA_all_relevant, "WDPAID \"WDPAID\" true true false 8 Double 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/WDPA_poly_relevant,WDPAID,-1,-1,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/WDPA_point_relevant_with_joined_count_multiparts_buffered,WDPAID,-1,-1;ISO3 \"ISO3\" true true false 50 Text 0 0 ,First,#,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/WDPA_poly_relevant,ISO3,-1,-1,Z:/globes/USERS/GIACOMO/protconn/data/ProtConn_Mar2020.gdb/WDPA_point_relevant_with_joined_count_multiparts_buffered,ISO3,-1,-1" ) print("Points and polygons merged") # Process: Repair Geometry arcpy.RepairGeometry_management(WDPA_all_relevant, "DELETE_NULL") print("Geometries repaired") # Process: Create SQLite Database if arcpy.Exists(wdpa_for_protconn_gpkg): print(wdpa_for_protconn_gpkg, " already exists.") else: arcpy.CreateSQLiteDatabase_management(wdpa_for_protconn_gpkg, "GEOPACKAGE") # Process: Feature Class to Feature Class arcpy.FeatureClassToFeatureClass_conversion(WDPA_all_relevant, wdpa_for_protconn_gpkg, "wdpa_all_relevant") print("Features exported in Geopackage") print('-------------------------------------------------------') endtime = datetime.now() totaltime = endtime - firststarttime print(' ') print('PROCEDURE COMPLETED. Elapsed time: ', totaltime) print( 'Now execute in docker the script "/globes/USERS/GIACOMO/protconn/scripts/gis_proc/exec_simplify_wdpa_all_relevant.sh" ' )