Created
September 7, 2018 15:51
-
-
Save sgibbes/af401bf4452e495a0252cc7fed7d7df8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zipfile | |
import arcpy | |
import os | |
import glob | |
import subprocess | |
import shutil | |
arcpy.env.overwriteOutput = "TRUE" | |
def unzipfiles(myzipfile): | |
print "unzipping file" | |
# create a folder with the name of the file | |
extract_dir = myzipfile.split(".")[0] | |
# create the zip object based on zipped file | |
zip_ref = zipfile.ZipFile(myzipfile, 'r') | |
# extract file into the folder | |
zip_ref.extractall(extract_dir) | |
zip_ref.close() | |
return extract_dir | |
def int_w_gadm(final_outfolder, shapefile, gadm28): | |
filename = os.path.basename(shapefile).split(".")[0]+"_int_diss_gadm28.shp" | |
final_file = os.path.join(final_outfolder, filename) | |
desc = arcpy.Describe(shapefile) | |
extent = desc.extent | |
spatialReference = desc.spatialReference.name | |
if spatialReference != "GCS_WGS_1984": | |
print "projecting..." | |
projected = shapefile.split(".")[0]+"_projected.shp" | |
arcpy.Project_management(shapefile, projected, arcpy.SpatialReference(4326)) | |
shapefile = projected | |
print "intersecting..." | |
intersected = shapefile.split(".")[0]+"_intersected.shp" | |
print intersected | |
arcpy.Intersect_analysis([shapefile, gadm28], intersected) | |
print "dissolving..." | |
arcpy.Dissolve_management(intersected, final_file, ['ISO','ID_1', 'ID_2'], "", "SINGLE_PART") | |
print "finished." | |
return final_file | |
def convert_tsv(shapefile): | |
tsv_script = r"U:\sgibbes\GitHub\raster-vector-to-tsv\write-tsv.py" | |
filename = os.path.basename(shapefile).split(".")[0]+".tsv" | |
output_tsv = r"U:\sgibbes\GitHub\raster-vector-to-tsv\output\output.tsv" | |
local_tsv_dir = r'U:\sgibbes\convert_shape_to_tsv\tsv_files' | |
local_tsv = os.path.join(local_tsv_dir, filename) | |
if not os.path.exists(local_tsv): | |
cmd = "C:\Python27\ArcGISx6410.4\python {0} --main --threads 2 --step --function tsv --dataset1 {1} --id-field1 ISO".format(tsv_script, shapefile) | |
subprocess.check_call(cmd) | |
shutil.copy(output_tsv, local_tsv) | |
else: | |
print "tsv exists, process complete." | |
gadm28 = r'U:\sgibbes\gadm28_adm2_final\gadm28_adm2_final\adm2_final.shp' | |
final_outfolder = r'U:\sgibbes\convert_shape_to_tsv\prepped_files' | |
shapefile = r"U:\sgibbes\convert_shape_to_tsv\gfw_managed_forests\gfw_managed_forests.shp" | |
final_file = int_w_gadm(final_outfolder, shapefile, gadm28) | |
#convert_tsv(final_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment