Exemple #1
0
def testImport(proValue=True):
    ''' ideally code here should run without error
    '''
    # import importlib.util
    archook.get_arcpy(pro=proValue)
    import arcpy
    LOGGER.info("ok to use arcpy")
Exemple #2
0
 def checkout(extension):
     try:
         import archook
         archook.get_arcpy()
         import arcpy
         arcpy.CheckOutExtension(extension)
         return arcpy
     except Exception as e:
         logging.debug(e)
         return None
Exemple #3
0
 def checkout(name):
     try:
         if name == Products.ARCINFO:   import arcinfo
         if name == Products.ARCEDITOR: import arceditor
         if name == Products.ARCVIEW:   import arcview
         import archook
         archook.get_arcpy()
         import arcpy
         return arcpy if arcpy.ProductInfo() != "NotInitialized" else None
     except Exception as e:
         logging.debug(e)
         return None
Exemple #4
0
def verifyArcpy():
    '''
    misc code used to verify that the arcpy import actually works
    '''
    archook.get_arcpy(pro=True)
    debug_arcscripting()
    import arcpy
    from arcpy import metadata as md

    dataPath = os.environ['TEST_GDB']
    arcpy.env.workspace = dataPath

    datasets = arcpy.ListDatasets("*", "Feature")
    LOGGER.debug("datasets: {0}".format(datasets))

    desc = arcpy.Describe(dataPath)
    featureclasses = arcpy.ListFeatureClasses(dataPath)
    LOGGER.debug("featureclasses: {0}".format(featureclasses))

    LOGGER.debug("describe data type: {0}".format(desc.dataType))
Exemple #5
0
def debug_arcscripting_pro():
    '''
    Extracting the code from E:\sw_nt\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgisscripting\__init__.py
    to try to better understand what the issue is with it.  That code cannot be
    modified... whatever we come up with needs to work with all the downstream
    code unmodified.
    '''
    archook.get_arcpy(pro=True)
    #active_pyd_pth = Path(__file__).parent.joinpath("_arcgisscripting.pyd")
    underbarfile = r'E:\sw_nt\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgisscripting\__init__.py'
    from pathlib import Path
    import importlib
    active_pyd_pth = Path(underbarfile).parent.joinpath("_arcgisscripting.pyd")
    LOGGER.debug("active_pyd_pth: {0}".format(active_pyd_pth))
    arcpypath = importlib.util.find_spec("arcpy")
    LOGGER.debug("arcpypath: {0}".format(arcpypath))
    pristine_pyd_pth = Path(
        importlib.util.find_spec("arcpy").origin).parents[3].joinpath(
            'bin', 'Python', 'envs', 'arcgispro-py3', 'Lib', 'site-packages',
            "arcgisscripting", "_arcgisscripting.pyd")
    LOGGER.debug('pristine_pyd_pth: {0}'.format(pristine_pyd_pth))
Exemple #6
0
import archook  #The module which locates arcgis
archook.get_arcpy()
import arcpy
from arcpy import env
import os, shutil

env.workspace = "C:/Users/hengstam/Desktop/Research/hengst_env"
masterlakefile = "/master_lakes/master_lake_file.shp"

# Library
folder = env.workspace + '/master_lakes/lakes/'
tempfolder = env.workspace + '/temp/'

# Delete everything in the library
for the_file in os.listdir(folder):

    # Find file
    file_path = os.path.join(folder, the_file)

    try:
        # Delete files
        if os.path.isfile(file_path):
            os.unlink(file_path)

    except Exception as e:
        # What's wrong?
        print(e)

# Delete everything in the library
for the_file in os.listdir(tempfolder):
##################################################################
# merge_ssurgo.py Apr 2015
# ritvik sahajpal ([email protected])
# 
##################################################################
import multiprocessing, constants, logging, fnmatch, os, csv

try:
    import archook
    archook.get_arcpy()
    import arcpy
except ImportError:
    logging.info('Missing ArcPY')

from arcpy.sa import *

# ArcGIS initialization
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
arcpy.env.extent = "MAXOF"

def delete_temp_files(files_to_delete):
    for fl in files_to_delete:
        logging.info('Deleting: '+fl)
        try:
            arcpy.Delete_management(fl,"")
        except:
            logging.info(arcpy.GetMessages())

def merge_ssurgo_rasters(st):
    files_to_delete = [] # Temporary files which will be deleted at the end
Exemple #8
0
    def check_arcpy(self):
        """Check if arcpy and which version is installed."""
        # using archook
        try:
            archook.get_arcpy()
            import arcpy
            esri_info = arcpy.GetInstallInfo()
            logger.info("ArcPy imported from ArcGIS {} v{} in ({})"
                        " - using archook"
                        .format(esri_info.get("ProductName"),
                                esri_info.get("Version"),
                                archook.locate_arcgis()))
            # end of method
            return True, esri_info
        except ImportError as e:
            logger.error(e)

        # 3rd party libraries
        try:
            import arcpy
            esri_info = arcpy.GetInstallInfo()
            logger.info("ArcPy imported from ArcGIS {} v{} in ({})".format(
                        esri_info.get("ProductName"),
                        esri_info.get("Version"),
                        esri_info.get("InstallDir")))
            # end of method
            return True, esri_info
        except RuntimeError:
            logger.error("ArcPy is installed, but not licensed.")
            return False, "ArcGIS is installed, but not licensed."
        except ImportError:
            logger.info("ArcGIS isn't in the SYSPATH."
                        " Trying to find it automatically.")
            # checks if ArcGIS is installed
            if not path.isdir(path.join(env.get("PROGRAMFILES(x86)"), "ArcGIS"))\
               and not path.isdir(path.join(env.get("PROGRAMFILES"), "ArcGIS")):
                logger.info("ArcGIS isn't installed on this computer.")
                return False, "ArcGIS isn't installed on this computer."
            else:
                arcgis_path = path.join(env.get("PROGRAMFILES(x86)", "PROGRAMFILES"), "ArcGIS")
                pass
            logger.info("ArcGIS is installed but not well configured.")
            # path to the last version of 10 branch
            v = max([i[-1] for i in listdir(path.realpath(arcgis_path)) if "Desktop10" in i])
            arcgis_path = path.join(arcgis_path, "Desktop10.{}".format(v))
            # adding paths to the environment
            sys.path.append(path.join(arcgis_path, "arcpy"))
            sys.path.append(path.join(arcgis_path, "bin"))
            sys.path.append(path.join(arcgis_path, "ArcToolbox\Scripts"))
            try:
                import arcpy
                import site
                esri_info = arcpy.GetInstallInfo()
                logger.info("ArcGIS configuration has been fixed.")
                logger.info("ArcGIS installation: {}".format(arcpy.ProductInfo()))
                if hasattr(sys, 'real_prefix'):
                    # inside a venv
                    logger.info("Executing inside a virtualenv. Nice!")
                    pypacks = [p for p in sys.path if p.endswith('site-packages')][-1]
                else:
                    # using system install
                    logger.info("Executing from the main Python install.")
                    pypacks = site.getsitepackages()[1]

                # creatring pth file for future runs
                with open(path.join(pypacks, 'arcpy.pth'), 'w') as pth_arcpy:
                    pth_arcpy.write(path.realpath(path.join(arcgis_path, "arcpy")) + "\n")
                    pth_arcpy.write(path.realpath(path.join(arcgis_path, "bin")) + "\n")
                    pth_arcpy.write(path.realpath(path.join(arcgis_path, "ArcToolbox\Scripts")) + "\n")
                # end of method
                return True, esri_info
            except:
                logger.info("ArcGIS automatic configuration failed.")
                return False, "ArcGIS automatic configuration failed."
        else:
            logger.info("ArcGIS isn't installed on this computer.")
            return False, "ArcGIS isn't installed on this computer."
Exemple #9
0
'''
Created on Aug 15, 2014

@author: kjnether

Some simple code used to read information from esri layer files.  Primarily
used to populate GWIP tables used by the custom layer tool.

'''
import logging
import os
import platform
import archook

# setup for arcpro paths
archook.get_arcpy(pro=True)
import arcpy

class ReadLayerFiles(object):
    '''
    class used to support reading information from esri layer files.

    functionality included to help with creation of connections to SDE
    '''

    def __init__(self, lyrLib, scrtchSpc, user, passwd):
        self.logger = logging.getLogger(__name__)
        self.version = "9.3"  # other values: "10.0", "10.1", "10.2"
        self.lyrFileRootDir = lyrLib
        self.dataDir = scrtchSpc
        self.user = user
Exemple #10
0
# manual test (not a unit test)
import archook

r = archook.get_arcpy()
print("ArcGIS Desktop archook result: %s" % r)

r = archook.get_arcpy(pro=True)
print("ArcGIS Pro archook result: %s" % r)
Exemple #11
0
def main():

    ###############################################################################
    ##   USER INPUTS   ############################################################
    ###############################################################################
    # Define Longterm Lake Elevation (feet)
    Ontario = 245.5
    # Erie    = 571
    lake_elevation = Ontario

    # Define Buffer Distances
    topo_buffer_distance = "50 Feet"
    bathy_buffer_distance = "330 Feet"
    point_buffer_distance = "0.1 Feet"

    # Point to layers
    # Data masks
    masksdir = "P:/02/NY/Great Lakes Coastal/Terrain_Development/Oswego/masks/"
    topo_mask = masksdir + "Oswego_topo_mask.shp"
    bathy_mask = masksdir + "Oswego_bathy_mask.shp"

    # Source data
    geodatabase = "P:/02/NY/Oswego_Co_36075/STUDY__TO90/GIS/DATA/TOPO/Terrain/Oswego_Terrain_ft.gdb/"
    topo_data = geodatabase + "Topo_2014_lidar_C"
    bathy_data = geodatabase + "Bathy_Lidar_2011_C"
    shoreline = geodatabase + "Oswego_delineated_Shoreline_3D"

    # Point to temporary working directory
    temp_directory = "C:/Users/jdorvinen/Documents/Great_Lakes/Oswego/"

    ###############################################################################
    ##   IMPORT MODULES   #########################################################
    ###############################################################################
    import os
    import shapefile
    import archook

    archook.get_arcpy()
    import arcpy
    from datetime import datetime
    from time import strftime

    ###############################################################################
    ##   BEGIN SCRIPT   ###########################################################
    ###############################################################################
    begin = datetime.now()
    # Enter temporary working directory
    os.chdir(temp_directory)

    ###########################################################################
    # Buffer Shoreline
    ###########################################################################
    print("Buffering shoreline " + strftime("%Y-%m-%d %H:%M:%S"))
    topo_buffer = temp_directory + "shoreline_topo_buffer.shp"
    bathy_buffer = temp_directory + "shoreline_bathy_buffer.shp"
    arcpy.Buffer_analysis(shoreline, topo_buffer, topo_buffer_distance, "FULL", "ROUND", "NONE", "#")
    arcpy.Buffer_analysis(shoreline, bathy_buffer, bathy_buffer_distance, "FULL", "ROUND", "NONE", "#")
    print("done")

    ###########################################################################
    # Clip data sources to shoreline buffer
    ###########################################################################
    topo_clip = temp_directory + "topo_clip.shp"
    bathy_clip = temp_directory + "bathy_clip.shp"
    print("Clipping topo data to shoreline buffer " + strftime("%Y-%m-%d %H:%M:%S"))
    arcpy.Clip_analysis(topo_data, topo_buffer, topo_clip, "#")
    print("done")

    print("Clipping bathy data to shoreline buffer " + strftime("%Y-%m-%d %H:%M:%S"))
    arcpy.Clip_analysis(bathy_data, bathy_buffer, bathy_clip, "#")
    print("done")

    ###########################################################################
    # Explode clipped multipoint data to singlepoints
    ###########################################################################
    topo_explode = temp_directory + "topo_explode.shp"
    bathy_explode = temp_directory + "bathy_explode.shp"
    print("Exploding clipped multipoint data " + strftime("%Y-%m-%d %H:%M:%S"))
    arcpy.MultipartToSinglepart_management(topo_clip, topo_explode)
    arcpy.MultipartToSinglepart_management(bathy_clip, bathy_explode)
    print("done")

    ###########################################################################
    # Select erroneous points from singlepoint topo and bathy layers
    ###########################################################################
    neg_onshore_remove = temp_directory + "neg_onshore_remove.shp"
    pos_offshore_remove = temp_directory + "pos_offshore_remove.shp"

    print("Selecting out erroneous data points " + strftime("%Y-%m-%d %H:%M:%S"))

    def find_bad_points(points_layer, cutoff_elevation, savefile, topo_bathy):
        sf = shapefile.Reader(points_layer)
        shapes = sf.shapes()
        zloc = []

        if topo_bathy == "topo":
            for i in range(len(shapes)):
                if shapes[i].z[0] < cutoff_elevation:
                    zloc.append(i)
        elif topo_bathy == "bathy":
            for i in range(len(shapes)):
                if shapes[i].z[0] > cutoff_elevation:
                    zloc.append(i)
        else:
            print("ERROR: must define local variable 'topo_bathy' as 'topo' or 'bathy'")

        w = shapefile.Writer(shapeType=11)
        w.autoBalance = 1
        w.field("OID", "C", "40")

        for i in zloc:
            x = shapes[i].points[0][0]
            y = shapes[i].points[0][1]
            z = shapes[i].z[0]
            w.point(x, y, z)
            w.record(OID=str(i))

        w.save(savefile)

    find_bad_points(topo_explode, lake_elevation, neg_onshore_remove, topo_bathy="topo")
    find_bad_points(bathy_explode, lake_elevation, pos_offshore_remove, topo_bathy="bathy")
    print("done")

    ###########################################################################
    # Buffer selected erroneous points from topo and bathy layers
    ###########################################################################
    neg_onshore_buffer = temp_directory + "neg_onshore_buffer.shp"
    pos_offshore_buffer = temp_directory + "pos_offshore_buffer.shp"

    print("Buffering selected points " + strftime("%Y-%m-%d %H:%M:%S"))
    arcpy.Buffer_analysis(neg_onshore_remove, neg_onshore_buffer, point_buffer_distance, "FULL", "ROUND", "NONE", "#")
    arcpy.Buffer_analysis(pos_offshore_remove, pos_offshore_buffer, point_buffer_distance, "FULL", "ROUND", "NONE", "#")
    print("done")

    ###########################################################################
    # Erase buffers from topo and bathy masks
    ###########################################################################
    topo_mask_v2 = temp_directory + "topo_mask_v2.shp"
    bathy_mask_v2 = temp_directory + "bathy_mask_v2.shp"

    print("Erasing buffered points from masks " + strftime("%Y-%m-%d %H:%M:%S"))
    arcpy.Erase_analysis(topo_mask, neg_onshore_buffer, topo_mask_v2, "#")
    arcpy.Erase_analysis(bathy_mask, pos_offshore_buffer, bathy_mask_v2, "#")
    print("done")
    # In a test run of Oswego County, NY (~33 miles of coastline) it took a
    # Total elapsed time of: 14:26:52.794000 to reach this point. Clipping is by
    # far the most time intensive of the operations performed. Initial clip of
    # source point data took ~14hrs. Expected total time to complete a county
    # of similar size to Oswego ~29hrs.

    ###########################################################################
    # Reclip source data to updated masks
    ###########################################################################
    """
    topo_data_v2  = temp_directory+"topo_data_v2.shp"
    bathy_data_v2 = temp_directory+"bathy_data_v2.shp"

    print("Clipping data to updated masks "+strftime("%Y-%m-%d %H:%M:%S"))
    arcpy.Clip_analysis(topo_data,
                        topo_mask_v2,
                        topo_data_v2,"#")
    arcpy.Clip_analysis(bathy_data,
                        bathy_mask_v2,
                        bathy_data_v2,"#")
    print("done")
    """
    ###########################################################################
    # Calculate total elapsed time
    ###########################################################################
    elapsed_time = datetime.now() - begin
    print("Script complete " + strftime("%Y-%m-%d %H:%M:%S"))
    print("Total elapsed time: " + str(elapsed_time))
import sys

# Add arcpy's directories to sys.path
from archook import get_arcpy
try:
    get_arcpy()
except ImportError:
    print 'Unable to locate arcpy directory'

# Additional tweaks to placate arcpy
from types import ModuleType
sys.path.append('./site-packages')  # Add a dummy site-packages folder to sys.path
sys.modules['numpy'] = ModuleType('numpy')  # Add a fake numpy module to sys.modules
Exemple #13
0
    def check_arcpy(self):
        """Check if arcpy and which version is installed."""
        # using archook
        try:
            archook.get_arcpy()
            import arcpy
            esri_info = arcpy.GetInstallInfo()
            logger.info("ArcPy imported from ArcGIS {} v{} in ({})"
                        " - using archook".format(esri_info.get("ProductName"),
                                                  esri_info.get("Version"),
                                                  archook.locate_arcgis()))
            # end of method
            return True, esri_info
        except ImportError as e:
            logger.error(e)

        # 3rd party libraries
        try:
            import arcpy
            esri_info = arcpy.GetInstallInfo()
            logger.info("ArcPy imported from ArcGIS {} v{} in ({})".format(
                esri_info.get("ProductName"), esri_info.get("Version"),
                esri_info.get("InstallDir")))
            # end of method
            return True, esri_info
        except RuntimeError:
            logger.error("ArcPy is installed, but not licensed.")
            return False, "ArcGIS is installed, but not licensed."
        except ImportError:
            logger.info("ArcGIS isn't in the SYSPATH."
                        " Trying to find it automatically.")
            # checks if ArcGIS is installed
            if not path.isdir(path.join(env.get("PROGRAMFILES(x86)"), "ArcGIS"))\
               and not path.isdir(path.join(env.get("PROGRAMFILES"), "ArcGIS")):
                logger.info("ArcGIS isn't installed on this computer.")
                return False, "ArcGIS isn't installed on this computer."
            else:
                arcgis_path = path.join(
                    env.get("PROGRAMFILES(x86)", "PROGRAMFILES"), "ArcGIS")
                pass
            logger.info("ArcGIS is installed but not well configured.")
            # path to the last version of 10 branch
            v = max([
                i[-1] for i in listdir(path.realpath(arcgis_path))
                if "Desktop10" in i
            ])
            arcgis_path = path.join(arcgis_path, "Desktop10.{}".format(v))
            # adding paths to the environment
            sys.path.append(path.join(arcgis_path, "arcpy"))
            sys.path.append(path.join(arcgis_path, "bin"))
            sys.path.append(path.join(arcgis_path, "ArcToolbox\Scripts"))
            try:
                import arcpy
                import site
                esri_info = arcpy.GetInstallInfo()
                logger.info("ArcGIS configuration has been fixed.")
                logger.info("ArcGIS installation: {}".format(
                    arcpy.ProductInfo()))
                if hasattr(sys, 'real_prefix'):
                    # inside a venv
                    logger.info("Executing inside a virtualenv. Nice!")
                    pypacks = [
                        p for p in sys.path if p.endswith('site-packages')
                    ][-1]
                else:
                    # using system install
                    logger.info("Executing from the main Python install.")
                    pypacks = site.getsitepackages()[1]

                # creatring pth file for future runs
                with open(path.join(pypacks, 'arcpy.pth'), 'w') as pth_arcpy:
                    pth_arcpy.write(
                        path.realpath(path.join(arcgis_path, "arcpy")) + "\n")
                    pth_arcpy.write(
                        path.realpath(path.join(arcgis_path, "bin")) + "\n")
                    pth_arcpy.write(
                        path.realpath(
                            path.join(arcgis_path, "ArcToolbox\Scripts")) +
                        "\n")
                # end of method
                return True, esri_info
            except:
                logger.info("ArcGIS automatic configuration failed.")
                return False, "ArcGIS automatic configuration failed."
        else:
            logger.info("ArcGIS isn't installed on this computer.")
            return False, "ArcGIS isn't installed on this computer."