cga-wm / advgis-beta

This is Advanced GIS at W&M - beta version.
0 stars 0 forks source link

Defining resources in notebooks for Pro #27

Open parthree opened 4 years ago

parthree commented 4 years ago

How does setting up a geodatabase in Pro differ from AGOL? I started a New Project in Pro and made a Geofencing folder as my default. Is that the same as home? So instead of my_dir = "/arcgis/home" I did my_dir = "\Geofencing"

It didn't work: GDBError1

dt-woods commented 4 years ago

One thing that stands out is the \Geofencing directory. In Python, if you begin a path with the slash, it thinks to looks at the root directory, which would be your C:\ drive; I'm guessing you don't have a C:\Geofencing directory. Check to see where your "Geofencing" directory lives on your computer (e.g., "C:\Users\user\Desktop\Geofencing"), update your my_dir variable, and see if you run into the same trouble.

parthree commented 4 years ago

my_dir = "C:\Classfolders\Adv GIS Summer 2020\Geofencing" did not work either. Because of the spaces? I tried putting % instead of spaces with no success.

dt-woods commented 4 years ago

Yeah. The old adage of "No spaces in files or folder names" applies to arcpy. Try renaming your "Adv GIS Summer 2020" folder without spaces (e.g., "AdvGIS_Su20").

parthree commented 4 years ago

Thanks! To create more than one feature class, would you recommend creating all of them first, then adding fields and domains, or creating one at a time and adding fields and domains as you go? If I created more than one feature class, should I assume the feat_name will refer to the most recent feature class created? Do I have to change the out_name for each feature class if making multiple ones before adding fields?

dt-woods commented 4 years ago

I think this is a personal preference. Either way, you will want to think out the layer geometries, attributes and attribute types you want created before doing your field survey.

To answer your other questions, feat_name is a convenience variable for defining the layer name (it's to try to avoid mis-matching names). For example, below I'm using feat_name to define the name of the feature layer I want to create, which in this case is "landmarks." I first check to see if it already exists in the feature collection. I then use feat_name variable for the out_name in the CreateFeatureclass_management function. This way, I'm using the same name for the existence check and feature class creation; otherwise, you are right, you need to manually put "landmarks" or whatever your layer name is for out_name.

feat_name = "landmarks"                           # define my feature layer name
if feat_name not in arcpy.ListFeatureClasses():   # check if this feature layer exists
    print("Creating new feature class...")
    arcpy.CreateFeatureclass_management(
        out_path = fgd_path,      # path to our file GDB
        out_name = feat_name,     # the name of our new feature (no spaces/special characters)
        out_alias = "POI",        # alias (doesn't have to follow naming rules)
        geometry_type = "POINT",  # geometry (point, line or polygon)
        spatial_reference = sr    # coordinate reference system
    )
else:
    print("'{}' already exists".format(feat_name))

print(arcpy.ListFeatureClasses())
dt-woods commented 4 years ago

If you are going to create multiple feature classes, you could define them all in a dictionary of dictionaries and loop through it. The inner dictionary would need to include at least "name," "alias," and "geometry," for example:

feat_dict = {
    1: {
        'name': 'landmarks',
        'alias': 'POI',
        'geometry': 'POINT'
        },
    2: {
        'name': 'landscapes',
        'alias': 'Garden landscapes',
        'geometry': 'POLYGON'
        }
}

You could then loop over this dictionary to get each bit of information.

for feat in feat_dict.values():
    feat_name = feat['name']
    feat_alias = feat['alias']
    feat_geom = feat['geometry']
    # Do existence check and create feature class
dt-woods commented 4 years ago

A side note: you don't have to automate everything. This was just to demonstrate how you could begin that process.