Exemple #1
0
def copyPipesegmenttoGascross():
    """
    本函数为将管段表中的穿跨越段连同其相关属性写入到穿跨越表中
    基本思路:
    首先从管段表中选出敷设方式为穿跨越的要素,因此要求填写管段表时,
        必须正确填写其敷设方式(如果为穿跨越必须将其敷设方式设置为穿越或跨越)
    然后将这些要素连同其属性添加到穿跨越表中
    """
    #从管段表中选择敷设方式为穿越/跨越的要素
    arcpy.Select_analysis("T_PN_PIPESEGMENT_GEO","ForCross","LAYMODE = 4 OR LAYMODE = 5")

    #新建字段映射列表对象
    fieldMappings = arcpy.FieldMappings()     #创建字段映射对象
    fieldTuple=("PIPENAME","PSCODE","PIPESEGNO","MSTART","MEND","USEDDATE","REFOBJSTART",\
                "OFFSETSTART","XSTART","YSTART","ZSTART","REFOBJEND","OFFSETEND",\
                "XEND","YEND","ZEND","CONSTRUNIT","SUPERVISORUNIT","TESTUNIT",\
                "FDNAME","INPUTDATETIME","COLLECTUNIT","COLLECTDATE","NAME")
    for FT in fieldTuple:
        if FT!="PSCODE" and FT!="PIPESEGNO":
            #新建字段映射对象
            fm=arcpy.FieldMap()        
            #将源字段添加入字段映射中
            fm.addInputField("ForCross",FT)
            #设置目标字段映射
            fm_name=fm.outputField
            fm_name.name = FT
            fm.outputField = fm_name
            #将字段映射放入字段映射列表中
            fieldMappings.addFieldMap(fm)
        elif FT=="PSCODE":
            #新建字段映射对象
            fm=arcpy.FieldMap()        
            #将源字段添加入字段映射中
            fm.addInputField("ForCross","CODE")
            #设置目标字段映射
            fm_name=fm.outputField
            fm_name.name = FT
            fm.outputField = fm_name
            #将字段映射放入字段映射列表中
            fieldMappings.addFieldMap(fm)
        elif FT=="PIPESEGNO":
            #新建字段映射对象
            fm=arcpy.FieldMap()        
            #将源字段添加入字段映射中
            fm.addInputField("ForCross","NAME")
            #设置目标字段映射
            fm_name=fm.outputField
            fm_name.name = FT
            fm.outputField = fm_name
            #将字段映射放入字段映射列表中
            fieldMappings.addFieldMap(fm)
            
    #将筛选出的穿跨越管段,添加到穿跨越表中
    arcpy.Append_management("ForCross","T_LP_GASCROSS_GEO","NO_TEST",fieldMappings,"")

    #删除中间文件
    arcpy.Delete_management("ForCross")

    #对穿跨越进行编码
    featureCoding("T_LP_GASCROSS_GEO")
    def merge_incl_mappings(fc_and_field_name_list, output_fc):
        fc_list = []  # used to keep track of FCs that will be merged
        field_mappings = arcpy.FieldMappings()
        review_type_field_map = arcpy.FieldMap()
        review_type_field_name = 'REVIEW_TYPE'
        sub_category_field_map = arcpy.FieldMap()
        for fc_and_field_name in fc_and_field_name_list:
            return_dict = add_input_fields_to_field_maps(
                fc_and_field_name[0], fc_and_field_name[1],
                review_type_field_map, sub_category_field_map,
                review_type_field_name, fc_and_field_name[2])
            fc_list.append(fc_and_field_name[1])
            review_type_field_map = return_dict["review_type_field_map"]
        outField = review_type_field_map.outputField
        outField.name = review_type_field_name
        outField.aliasName = review_type_field_name
        outField.length = 2000
        review_type_field_map.outputField = outField
        field_mappings.addFieldMap(review_type_field_map)

        outField = sub_category_field_map.outputField
        outField.name = 'SUB_CATEGORY'
        outField.aliasName = 'SUB_CATEGORY'
        outField.length = 2000
        sub_category_field_map.outputField = outField
        field_mappings.addFieldMap(sub_category_field_map)

        arcpy.Merge_management(fc_list, output_fc, field_mappings)

        print("Deleting feature classes from memory")
        for fc in fc_list:
            arcpy.Delete_management(fc)
Exemple #3
0
def find_states(fc, state_fc):
    """Populate *_states field. States fc must have field 'states' with length 255 and state abbreviations within."""
    states_field = '{}_states'.format(os.path.basename(fc))
    if arcpy.ListFields(fc, states_field):
        DM.DeleteField(fc, states_field)

    # reverse buffer the states slightly to avoid "D", "I", "J"  situations in "INTERSECT" illustration
    # from graphic examples of ArcGIS join types "Select polygon using polygon" section in Help


    # make a field mapping that gathers all the intersecting states into one new value
    field_list = [f.name for f in arcpy.ListFields(fc) if f.type <> 'OID' and f.type <> 'Geometry']
    field_mapping = arcpy.FieldMappings()
    for f in field_list:
        map = arcpy.FieldMap()
        map.addInputField(fc, f)
        field_mapping.addFieldMap(map)
    map_states = arcpy.FieldMap()
    map_states.addInputField(state_fc, 'states')
    map_states.mergeRule = 'Join'
    map_states.joinDelimiter = ' '
    field_mapping.addFieldMap(map_states)

    # perform join and use output to replace original fc
    spjoin = AN.SpatialJoin(fc, state_fc, 'in_memory/spjoin_intersect', 'JOIN_ONE_TO_ONE',
                            field_mapping=field_mapping, match_option='INTERSECT')
    DM.AlterField(spjoin, 'states', new_field_name=states_field, clear_field_alias=True)
    DM.Delete(fc)
    DM.CopyFeatures(spjoin, fc)
    DM.Delete(spjoin)
Exemple #4
0
def join_table_shapefile(table, tablefield, shapefile, shapefield, outputname):
    # This functino joins a table and feature class ("shapefile" based on the
    # join key in "tablefield" and "shapefield." Saves the joined result as a
    # new feature class named "outputname."

    # Create spatial dataset to Feature Layer
    shape_layer = AutoName(shapefile + '_table')
    arcpy.MakeFeatureLayer_management(shapefile,
                                      shape_layer,
                                      workspace=workspace)

    # Add a join from the table to the newly created feature layer
    temp_join = arcpy.AddJoin_management(shape_layer, shapefield, table,
                                         tablefield, 'KEEP_ALL')
    tempout = AutoName('tempout')
    # To make the join permanent, copy the result to a new feature class "tempout"
    arcpy.CopyFeatures_management(temp_join, tempout)

    # Now clean up field names, which at present are messy
    #   Get names as strings and remove the $ in Excel sheet names
    table_name = os.path.basename(os.path.normpath(table))
    shapefile_name = os.path.basename(os.path.normpath(shapefile))
    table_name = table_name.replace("$", "_")
    table_name_plus = table_name + '*'
    shapefile_name = shapefile_name.replace("$", "_")
    shapefile_name_plus = shapefile_name + '*'

    # Get all field names in result feature class
    fms = arcpy.FieldMappings()

    for field in arcpy.ListFields(tempout, table_name_plus):
        fm = arcpy.FieldMap()
        fm.addInputField(tempout, field.name)
        fname = fm.outputField
        fname.name = field.name[(len(table_name) + 1):]
        fname.alias = fname.name
        fm.outputField = fname
        fms.addFieldMap(fm)

    for field in arcpy.ListFields(tempout, shapefile_name_plus):
        fm = arcpy.FieldMap()
        fm.addInputField(tempout, field.name)
        fname = fm.outputField
        fname.name = field.name[(len(shapefile_name) + 1):]
        fname.aliasName = fname.name
        fm.outputField = fname
        fms.addFieldMap(fm)

    arcpy.FeatureClassToFeatureClass_conversion(tempout,
                                                arcpy.env.workspace,
                                                outputname,
                                                field_mapping=fms)

    # Delete intermediate products
    arcpy.Delete_management(tempout)

    return (outputname)
def join_table_shapefile(table, tablefield, shapefile, shapefield, outputname):

    # Join luloadtable to bmpparcels to get code from 3-12
    shape_layer = AutoName(shapefile + '_table')
    arcpy.MakeFeatureLayer_management(shapefile,
                                      shape_layer,
                                      workspace=workspace)

    # Add a join from the pollutant-relevant land use type to parcel database
    temp_join = arcpy.AddJoin_management(shape_layer, shapefield, table,
                                         tablefield, 'KEEP_ALL')
    tempout = AutoName('tempout')
    arcpy.CopyFeatures_management(temp_join, tempout)

    # Now fix field names.
    #   Get names as strings and remove the $ in Excel sheet names
    table_name = os.path.basename(os.path.normpath(table))
    shapefile_name = os.path.basename(os.path.normpath(shapefile))
    table_name = table_name.replace("$", "_")
    table_name_plus = table_name + '*'
    shapefile_name = shapefile_name.replace("$", "_")
    shapefile_name_plus = shapefile_name + '*'

    # Get all field names in result feature class

    fms = arcpy.FieldMappings()

    for field in arcpy.ListFields(tempout, table_name_plus):
        fm = arcpy.FieldMap()
        fm.addInputField(tempout, field.name)
        fname = fm.outputField
        fname.name = field.name[(len(table_name) + 1):]
        fname.alias = fname.name
        fm.outputField = fname
        fms.addFieldMap(fm)

    for field in arcpy.ListFields(tempout, shapefile_name_plus):
        fm = arcpy.FieldMap()
        fm.addInputField(tempout, field.name)
        fname = fm.outputField
        fname.name = field.name[(len(shapefile_name) + 1):]
        fname.aliasName = fname.name
        fm.outputField = fname
        fms.addFieldMap(fm)

    #fms.addFieldMap(fm)

    arcpy.FeatureClassToFeatureClass_conversion(tempout,
                                                arcpy.env.workspace,
                                                outputname,
                                                field_mapping=fms)

    # Delete intermediate products
    arcpy.Delete_management(tempout)

    return (outputname)
def main(input_strm, input_seg, outFGB):

    arcpy.AddMessage(
        "Transferring original stream reach object IDs to segments features..."
    )

    # create copy of stream segments, which will include new stream ID field
    seg_id = arcpy.FeatureClassToFeatureClass_conversion(
        input_seg, outFGB, r"seg_id")

    # generate the midpoint for each segmented stream network
    arcpy.FeatureToPoint_management(input_seg, outFGB + "\midpoint", "INSIDE")
    arcpy.MakeFeatureLayer_management(outFGB + "\midpoint", "midpoint_lyr")

    # field mapping for the spatial join (this is from ESRI help resources example)
    # create FieldMap and FieldMapping objects
    fm_strmOID = arcpy.FieldMap()
    fm_segOID = arcpy.FieldMap()
    fms = arcpy.FieldMappings()
    # set field names from input files
    strm_oid = arcpy.Describe(input_strm).OIDFieldName
    line_oid = "LineOID"  # from seg_id
    # add fields to FieldMap objects
    fm_strmOID.addInputField(input_strm, strm_oid)
    fm_segOID.addInputField(seg_id, line_oid)
    # set output field properties for FieldMap objects (this is pure ESRI clunkiness)
    strmOID_name = fm_strmOID.outputField
    strmOID_name.name = "strmOID"
    fm_strmOID.outputField = strmOID_name
    segOID_name = fm_segOID.outputField
    segOID_name.name = "LineOID"
    fm_segOID.outputField = segOID_name
    # add FieldMap objects to the FieldMappings object
    fms.addFieldMap(fm_strmOID)
    fms.addFieldMap(fm_segOID)

    # spatial join between midpoints and original stream network polyline feature class
    ##midpoint_join = r"in_memory\midpoint_join"
    midpoint_join = outFGB + "\midpoint_join"
    arcpy.SpatialJoin_analysis("midpoint_lyr", input_strm, midpoint_join,
                               "JOIN_ONE_TO_MANY", "KEEP_ALL", fms,
                               "INTERSECT", 0.5)

    # join midpoints to segmented streams based on shared LineOID
    arcpy.MakeFeatureLayer_management(seg_id, "seg_id_lyr")
    arcpy.AddField_management("seg_id_lyr", "strmID", "TEXT")
    arcpy.AddJoin_management("seg_id_lyr", "LineOID", midpoint_join, "LineOID",
                             "KEEP_ALL")
    arcpy.CalculateField_management("seg_id_lyr", "strmID", "!strmOID!",
                                    "PYTHON_9.3")
    arcpy.RemoveJoin_management("seg_id_lyr")

    return seg_id
Exemple #7
0
def wetlands_in_zones(infolder, idfield, wetlands, top_outfolder):

    # Create output geodatabase in outfolder
    out_gdb = os.path.join(top_outfolder, "WetlandsInZones.gdb")
    if not arcpy.Exists(out_gdb):
        arcpy.CreateFileGDB_management(top_outfolder, "WetlandsInZones")

    # Add WetlandHa field if it doesn't exist
    if len(arcpy.ListFields(wetlands, 'WetlandHa')) == 0:
        arcpy.AddField_management(wetlands, "WetlandHa", "DOUBLE")
    expha = "!shape.area@hectares!"
    arcpy.CalculateField_management(wetlands, "WetlandHa", expha, "PYTHON")

    # Set in memory as workspace. Intermediate output will be held in RAM.
    mem = "in_memory"
    arcpy.env.workspace = mem

    # Make wetlands in memory.
    exp = """"ATTRIBUTE" LIKE 'P%'AND "WETLAND_TY" <> 'Freshwater_Pond'"""
    arcpy.Select_analysis(wetlands, "wetlandspoly", exp)

    # Convert wetlands to points
    arcpy.FeatureToPoint_management("wetlandspoly", "wetlands", "INSIDE")

    # List extent feature classes
    fcs = []
    for root, dirs, files in arcpy.da.Walk(infolder):
        for file in files:
            fcs.append(os.path.join(root, file))

    # Spatial Join the wetlands to each extent
    out_fcs = []
    for fc in fcs:
        cu.multi_msg("Creating results for %s" % fc)
        name = os.path.basename(fc)
        fms = arcpy.FieldMappings()
        fmid = arcpy.FieldMap()
        fmha = arcpy.FieldMap()
        fmid.addInputField(fc, idfield)
        fmha.addInputField("wetlands", "WetlandHa")
        fmha.mergeRule = 'Sum'
        fms.addFieldMap(fmid)
        fms.addFieldMap(fmha)
        out_fc = os.path.join(out_gdb, name + "_Wetlands")
        arcpy.SpatialJoin_analysis(fc, wetlands, out_fc, '', '', fms)
        out_fcs.append(out_fc)

    # Export feature classes attribute tables to tables
    for f in out_fcs:
        arcpy.CopyRows_management(
            f, os.path.join(out_gdb, "Table" + os.path.basename(f)))
Exemple #8
0
def mapFields(inlayer, infield, mapfield_name, mapfield_alias, mapfield_type): # mapFields function
    fldMap = arcpy.FieldMap()
    fldMap.addInputField(inlayer, infield)
    mapOut = fldMap.outputField
    mapOut.name, mapOut.alias, mapOut.type = mapfield_name, mapfield_alias, mapfield_type
    fldMap.outputField = mapOut
    return fldMap
    def _export_od_lines(self):
        """Export OD lines to an output feature class.

        Returns:
            The full catalog path of the output feature class.

        """
        self.logger.debug("Exporting OD lines")
        travel_time_field_name = "Total_{}".format(self.time_attribute)
        travel_distance_field_name = "Total_{}".format(self.distance_attribute)
        fields_to_copy = (self.ORIGINS_OID_FIELD_NAME, self.DESTINATIONS_OID_FIELD_NAME, "DestinationRank",
                          travel_time_field_name, travel_distance_field_name)
        field_mappings = arcpy.FieldMappings()
        for field in fields_to_copy:
            field_map = arcpy.FieldMap()
            field_map.addInputField(self.lines_sublayer, field)
            if field == travel_time_field_name:
                output_field = field_map.outputField
                output_field.name = "Total_Time"
                output_field.aliasName = "Total Time"
                field_map.outputField = output_field
            elif field == travel_distance_field_name:
                output_field = field_map.outputField
                output_field.name = "Total_Distance"
                output_field.aliasName = "Total Distance"
                field_map.outputField = output_field
            else:
                pass
            field_mappings.addFieldMap(field_map)

        result = arcpy.conversion.FeatureClassToFeatureClass(self.lines_sublayer, self.od_workspace, "output_od_lines",
                                                             field_mapping=field_mappings)
        self.logger.debug("Exporting OD lines %s", result.getMessages().split("\n")[-1])
        return result.getOutput(0)
def fields_to_table(input_data, fields, workspace, output_table):
    '''function creates field mappings object then uses the table to tables
    conversion tool to export a table to your gdb from your FC fields
    :param input_data: your feature class in your geodatabase
    :param fields: a list of your field names (string)
    :param workspace: your arcpy.env.workspace
    :output_table: the name of your output table'''

    #create empty field mapping object
    fms = arcpy.FieldMappings()

    #define fieldmaps and add to field mapping objects (fms)
    for field in fields:
        fm = arcpy.FieldMap()
        fm.addInputField(input_data, field)
        output_field = fm.outputField
        output_field.name = field
        fm.outputField = output_field

        # add the field map to the field mappings object
        fms.addFieldMap(fm)

    #print field map object
    print(fms)

    #run table to table conversion tool with field mappings parameter
    arcpy.TableToTable_conversion(input_data, workspace, output_table, "", fms)
    print("converted %s to table" %(input_data))
    def fields_to_lower(input_feature_class, workspace, output_feature_class,
                        output_save_location):
        print('Copying input data source' + input_feature_class +
              ' into memory')
        arcpy.FeatureClassToFeatureClass_conversion(input_feature_class,
                                                    workspace,
                                                    output_feature_class)
        field_names = [f.name for f in arcpy.ListFields(output_feature_class)]
        print('Building Field Maps')
        field_mappings = arcpy.FieldMappings()
        for field_name in field_names:
            field_map = arcpy.FieldMap()
            if field_name != 'Shape':  # https://gis.stackexchange.com/questions/30370/arcpy-error-in-adding-input-field-to-field-map
                field_map.addInputField(output_feature_class, field_name)
                outField = field_map.outputField
                outField.name = field_name.lower()
                field_map.outputField = outField
                field_mappings.addFieldMap(field_map)

        print('Field Maps built')

        print('Saving ' + output_feature_class +
              ' to PR_Flags_Phil geodatabase')
        arcpy.FeatureClassToFeatureClass_conversion(
            output_feature_class,
            output_save_location,
            output_feature_class,
            field_mapping=field_mappings)
        print('Saved ' + output_feature_class +
              ' to PR_Flags_Phil geodatabase')

        arcpy.Delete_management(output_feature_class)
Exemple #12
0
def generate_statistical_fieldmap(target_features,
                                  join_features,
                                  prepended_name="",
                                  merge_rule_dict={}):
    """Generates field map object based on passed field objects based on passed tables (list),
    input_field_objects (list), and passed statistics fields to choose for numeric and categorical variables. Output
    fields take the form of *merge rule*+*prepended_name*+*fieldname*
    :params
    target_features(str): target feature class that will maintain its field attributes
    join_features(str): join feature class whose numeric fields will be joined based on the merge rule dictionary
    prepended_name(str): modifies output join fields with param text between the statistics and the original field name
    merge_rule_dict (dict): a  dictionary of the form {statistic_type:[Fields,To,Summarize]}
    :returns arcpy field mapping object"""
    field_mappings = arcpy.FieldMappings()
    # We want every field in 'target_features' and all fields in join_features that are present
    # in the field statistics mappping.
    field_mappings.addTable(target_features)
    for merge_rule in merge_rule_dict:
        for field in merge_rule_dict[merge_rule]:
            new_field_map = arcpy.FieldMap()
            new_field_map.addInputField(join_features, field)
            new_field_map.mergeRule = merge_rule
            out_field = new_field_map.outputField
            out_field.name = str(merge_rule) + str(prepended_name) + str(field)
            out_field.aliasName = str(merge_rule) + str(prepended_name) + str(
                field)
            new_field_map.outputField = out_field
            field_mappings.addFieldMap(new_field_map)
    return field_mappings
Exemple #13
0
def SpatJoin_1to1_Closest(Out_Field, Dist_Field, Join_Fields_list, fc_target, fc_join, out_fc, search_distance):

    # Create a new fieldmappings object including field maps for all columns in fc_target
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.addTable(fc_target)

    # remove Join_Count and TARGET_FID from field mappings
    list = [x.name for x in fieldmappings.fields]
    if u'Join_Count' in list:
        fieldmappings.removeFieldMap(list.index(u'Join_Count'))
        list.remove(u'Join_Count')
    if u'TARGET_FID' in list:
        fieldmappings.removeFieldMap(list.index(u'TARGET_FID'))

    # Create field map for the new column that gets added by spatial join.
    FieldMap = arcpy.FieldMap()
    # Set input fields from fc_join
    for x in Join_Fields_list:
        FieldMap.addInputField(fc_join, x)
    # Edit output field properties
    field = FieldMap.outputField #get field with current properties
    if field.type == u'String':
        field.length = 3000
    field.name = Out_Field
    field.aliasName = Out_Field
    FieldMap.outputField = field #overwrite field with editted properties

    # Add field map to field mappings.
    fieldmappings.addFieldMap(FieldMap)

    # Run the Spatial Join tool.
    arcpy.SpatialJoin_analysis(fc_target, fc_join, out_fc,"JOIN_ONE_TO_ONE","KEEP_ALL",fieldmappings,"CLOSEST",search_distance,Dist_Field)
Exemple #14
0
def get_field_map(src, flds):
    """
    Returns a field map for an arcpy data itme from a list or dictionary.
    Useful for operations such as renaming columns merging feature classes.

    Parameters:
    -----------
    src: str, arcpy data item or arcpy.mp layer or table
        Source data item containing the desired fields.
    flds: dict <str: str>
        Mapping between old (keys) and new field names (values).

    Returns:
    --------
    arcpy.FieldMappings

    """
    mappings = arcpy.FieldMappings()
    if isinstance(flds, list):
        flds = {n: n for n in flds}

    for old_name, new_name in flds.items():
        fm = arcpy.FieldMap()
        fm.addInputField(src, old_name)
        out_f = fm.outputField
        out_f.name = new_name
        out_f.aliasName = new_name
        fm.outputField = out_f
        fm.outputField.name = new_name
        mappings.addFieldMap(fm)

    return mappings
Exemple #15
0
    def getFieldMap(self, filename):
        outFieldMap = arcpy.FieldMap()
        outFieldMap.addInputField(filename, self.nameFull)
        outField = outFieldMap.outputField
        outField.name = self.nameShort
        outFieldMap.outputField = outField

        return outFieldMap
def field_map_x(input_fc, field_name, output_field_name):
    field_map_x = arcpy.FieldMap()  # create the field map object
    field_map_x.addInputField(
        input_fc, field_name)  # add the input field name to the map
    field_x_output = field_map_x.outputField  # create the outputfield object
    field_x_output.name = output_field_name  # give the output field a name
    field_map_x.outputField = field_x_output  # copy named output field back
    return field_map_x
Exemple #17
0
def merge_feature(in_file1, in_file2, output_file):
    """
    Args: dir or name 
    """
    
    # Create the required FieldMap and FieldMappings objects
    fm_num_Doc = arcpy.FieldMap()
    fm_suit = arcpy.FieldMap()
    fms = arcpy.FieldMappings()
    
    # Get the field names of vegetation type and suiteter for both original
    # files
    file1_num_Doc = "num_Doc"
    file2_num_Doc = "num_Doc"
    
    file1_suit = "suit"
    file2_suit = "suit"
    
    # Add fields to their corresponding FieldMap objects
    fm_num_Doc.addInputField(in_file1, file1_num_Doc)
    fm_num_Doc.addInputField(in_file2, file2_num_Doc)
    
    fm_suit.addInputField(in_file1, file1_suit)
    fm_suit.addInputField(in_file2, file2_suit)
    
    # Set the output field properties for both FieldMap objects
    num_Doc_name = fm_num_Doc.outputField
    num_Doc_name.name = 'num_Doc'
    fm_num_Doc.outputField = num_Doc_name
    
    suit_name = fm_suit.outputField
    suit_name.name = 'suit'
    fm_suit.outputField = suit_name
    
    # Add the FieldMap objects to the FieldMappings object
    fms.addFieldMap(fm_num_Doc)
    fms.addFieldMap(fm_suit)
    
    if os.path.exists(output_file):
        arcpy.Delete_management(output_file)
    
    # Merge the two feature classes
    arcpy.Merge_management([in_file1, in_file2], output_file, fms)
    
    return arcpy.FeatureSet(output_file)
Exemple #18
0
    def extract(self):
        """Extract review features.

        Returns:
            arcetl.diff.Differ: Reference to instance.
        """
        # Clear old attributes.
        self._id_attr.clear()
        for tag in self._dataset_tags:
            feats = attributes.as_dicts(
                dataset_path=self._dataset[tag]["path"],
                field_names=self._keys["load"],
                dataset_where_sql=self._dataset[tag]["where_sql"],
                spatial_reference_item=self._dataset["init"]["spatial_reference"],
            )
            for feat in feats:
                id_val = tuple(freeze_values(*(feat[key] for key in self._keys["id"])))
                self._id_attr[tag][id_val] = feat
        # Add overlay attributes.
        for tag in self._dataset_tags:
            view = DatasetView(
                dataset_path=self._dataset[tag]["path"],
                dataset_where_sql=self._dataset[tag]["where_sql"],
                field_names=self._keys["id"],
            )
            with view:
                for overlay in self._dataset["overlays"]:
                    field_maps = arcpy.FieldMappings()
                    for path, keys in [
                        (view.name, self._keys["id"]),
                        (overlay["path"], overlay["keys"]),
                    ]:
                        for key in keys:
                            field_map = arcpy.FieldMap()
                            field_map.addInputField(path, key)
                            field_maps.addFieldMap(field_map)
                    output_path = unique_path()
                    arcpy.analysis.SpatialJoin(
                        target_features=view.name,
                        join_features=overlay["path"],
                        out_feature_class=output_path,
                        field_mapping=field_maps,
                    )
                    for feat in attributes.as_dicts(
                        output_path, field_names=(self._keys["id"] + overlay["keys"])
                    ):
                        id_val = tuple(
                            freeze_values(*(feat[key] for key in self._keys["id"]))
                        )
                        if id_val in self._id_attr[tag]:
                            for key in overlay["keys"]:
                                # Use (path, field name) for attribute key.
                                self._id_attr[tag][id_val][
                                    (overlay["path"], key)
                                ] = feat[key]
                    arcpy.management.Delete(output_path)
        return self
def shp2fc_sel_fields(in_shp, field_list, out_dir, out_temp_fc):
    field_maps = arcpy.arcpy.FieldMappings()
    
    for field in field_list:
        vars()[field] = arcpy.FieldMap() #vars() 
        vars()[field].addInputField(in_shp, field)
        field_maps.addFieldMap(vars()[field])
        
    arcpy.FeatureClassToFeatureClass_conversion(in_shp, out_dir, out_temp_fc, "", field_maps)
Exemple #20
0
def addAttributeFromPolygons(v_layer, v_field, v_joinLayer, v_joinField):
    env.workspace = os.path.dirname(v_layer)

    #check fields of join layer
    if(checkOrMakeField(v_joinLayer,v_joinField, False,"")==0):
        arcpy.AddError("Join Field Does Not Exist")
        sys.exit(1)
        
    #test if v_field exists, if not make it
    for field in arcpy.ListFields(v_joinLayer):
        if field.name == v_joinField:
            checkOrMakeField(v_layer,v_field, True,field.type)
        
     
    #add ID field for future us 
        
    #create field map
    fieldMappings = arcpy.FieldMappings()
    fieldMappings.removeAll()
    for item in arcpy.ListFields(v_layer):
        if item.type != "Geometry":
            fieldmap = arcpy.FieldMap()
            fieldmap.addInputField(v_layer, item.name)
            if item.name == v_field:
                fieldmap.addInputField(v_joinLayer,v_joinField)
            field = fieldmap.outputField
            field.name =item.name
            fieldmap.outputField = field
            fieldMappings.addFieldMap(fieldmap)
            
           
    #perform spatial join
    arcpy.SpatialJoin_analysis(v_layer,v_joinLayer,env.workspace + "\\SJ_Layer","JOIN_ONE_TO_ONE","KEEP_ALL",fieldMappings, "WITHIN")
    
    #use a join to map new info over, create list of Fields to drop
    dropFields = []
    for field in arcpy.ListFields(v_layer):
        if field.type == "OID":
            jField = field.name
            dropFields.append(field.name+"_1")
        elif field.name == v_field:
            dropFields.append("Join_Count")
            dropFields.append(v_field+"_1")
        else:
            dropFields.append(field.name+"_1")
            
    dropFields.append("TARGET_FID")
        
    
    arcpy.JoinField_management(v_layer, jField,"SJ_Layer", jField)
    arcpy.CalculateField_management(v_layer,v_field,"!"+v_field+"_1!","PYTHON")
    arcpy.DeleteField_management(v_layer, dropFields)
    
    
    arcpy.Delete_management("SJ_Layer")
    
    return 1;
Exemple #21
0
def multiFieldMap(sources, names, mergeRule):
    map = arcpy.FieldMap()
    for i in range(len(sources)):
        map.addInputField(sources[i], names[i])
    map.mergeRule = mergeRule
    outFld = map.outputField
    outFld.name = names[0]
    outFld.alias = names[0]
    map.outputField = outFld
    return map
Exemple #22
0
def getRenameFieldMap(featurePath, currentName, newName):
    """Create a field map that does a basic field rename."""
    tempMap = arcpy.FieldMap()
    tempMap.addInputField(featurePath, currentName)

    tempName = tempMap.outputField
    tempName.name = newName
    tempName.aliasName = newName
    tempMap.outputField = tempName

    return tempMap
 def SpatialJoinLargestOverlap(self, target_features, join_features, out_fc, keep_all, spatial_rel):
     pp1 = self.ProgressPrinter.newProcess(inspect.stack()[0][3], 1, 1).start()
     if spatial_rel == "largest_overlap":
         # Calculate intersection between Target Feature and Join Features
         intersect = arcpy.analysis.Intersect([target_features, join_features], "intersect", "ONLY_FID", "", "INPUT")
         # Find which Join Feature has the largest overlap with each Target Feature
         # Need to know the Target Features shape type, to know to read the SHAPE_AREA oR SHAPE_LENGTH property
         geom = "AREA" if arcpy.Describe(target_features).shapeType.lower() == "polygon" and arcpy.Describe(join_features).shapeType.lower() == "polygon" else "LENGTH"
         fields = ["FID_{0}".format(os.path.splitext(os.path.basename(target_features))[0]),
                   "FID_{0}".format(os.path.splitext(os.path.basename(join_features))[0]),
                   "SHAPE@{0}".format(geom)]
         overlap_dict = {}
         with arcpy.da.SearchCursor(intersect, fields) as scur:
             pp2 = self.ProgressPrinter.newProcess("search for overlap", 1, 2).start()
             for row in scur:
                 row = (row[0], row[1], round(row[2]*1000000,6))
                 try:
                     if row[2] > overlap_dict[row[0]][1]:
                         overlap_dict[row[0]] = [row[1], row[2]]
                 except:
                     overlap_dict[row[0]] = [row[1], row[2]]
             pp2.finish()
         arcpy.Delete_management("intersect")
         # Copy the target features and write the largest overlap join feature ID to each record
         # Set up all fields from the target features + ORIG_FID
         fieldmappings = arcpy.FieldMappings()
         fieldmappings.addTable(target_features)
         fieldmap = arcpy.FieldMap()
         fieldmap.addInputField(target_features, arcpy.Describe(target_features).OIDFieldName)
         fld = fieldmap.outputField
         fld.type, fld.name, fld.aliasName = "LONG", "ORIG_FID", "ORIG_FID"
         fieldmap.outputField = fld
         fieldmappings.addFieldMap(fieldmap)
         # Perform the copy
         arcpy.conversion.FeatureClassToFeatureClass(target_features, self.inventory.getWorkspace(), os.path.basename(out_fc), "", fieldmappings)
         # Add a new field JOIN_FID to contain the fid of the join feature with the largest overlap
         arcpy.management.AddField(out_fc, "JOIN_FID", "LONG")
         # Calculate the JOIN_FID field
         with arcpy.da.UpdateCursor(out_fc, ["ORIG_FID", "JOIN_FID"]) as ucur:
             pp2 = self.ProgressPrinter.newProcess("update rows", 1, 2).start()
             for row in ucur:
                 try:
                     row[1] = overlap_dict[row[0]][0]
                     ucur.updateRow(row)
                 except:
                     if not keep_all:
                         ucur.deleteRow()
             pp2.finish()
         # Join all attributes from the join features to the output
         pp2 = self.ProgressPrinter.newProcess("join fields", 1, 2).start()
         joinfields = [x.name for x in arcpy.ListFields(join_features) if not x.required]
         arcpy.management.JoinField(out_fc, "JOIN_FID", join_features, arcpy.Describe(join_features).OIDFieldName, joinfields)
         pp2.finish()
     pp1.finish()
Exemple #24
0
def add_multi_muni():
    # Build multi muni PSAP boundaries from muni boundaries
    print("Building multi muni PSAPs from muni boundaries ...")
    arcpy.management.CopyFeatures(psap_schema, multi_muni_temp)
    if arcpy.Exists("muni_lyr"):
        arcpy.management.Delete("muni_lyr")
    arcpy.management.MakeFeatureLayer(munis, "muni_lyr")
       
    # Field Map muni name into psap schema fields
    fms = arcpy.FieldMappings()
    
    # NAME to DsplayName
    fm_name = arcpy.FieldMap()
    fm_name.addInputField("muni_lyr", "NAME")
    output = fm_name.outputField
    output.name = "DsplayName"
    fm_name.outputField = output
    fms.addFieldMap(fm_name)
    
    # Build query to select multi muni 
    mm_list = [ item.split(',') for item in list(multi_muni_dict.values())]
    mm_list = [y.strip() for x in mm_list for y in x]
    print(mm_list)
    mm_query = f"NAME IN ({mm_list})".replace('[', '').replace(']', '')
    print(mm_query)
    
    # Complete the append with field mapping and query to get all munis in group
    arcpy.management.Append("muni_lyr", multi_muni_temp, "NO_TEST", field_mapping=fms, expression=mm_query)
    
    # Loop through and populate fields with appropriate information and rename to multi muni psaps
    update_count = 0
    fields = ['DsplayName']
    with arcpy.da.UpdateCursor(multi_muni_temp, fields) as update_cursor:
        print("Looping through rows in FC ...")
        for row in update_cursor:
            for k,v in multi_muni_dict.items():
                # print(f'key: {k}     value: {v}')
                if row[0] in v:
                    # print(f'Found {row[0]} in {v} ...')
                    row[0] = k
            update_count += 1
            update_cursor.updateRow(row)
    print(f"Total count of multi muni updates is: {update_count}")
        
    print("Dissolving multi muni PSAPs ...")
    arcpy.management.Dissolve(multi_muni_temp, mm_diss, "DsplayName")
    
    # Drop in multi muni psaps via erase/append
    print("Adding multi muni PSAPs into working psaps layer ...")
    # Erase
    arcpy.analysis.Erase(county_single_muni_temp, mm_diss, all_county_muni_temp)
    # Append
    arcpy.management.Append(mm_diss, all_county_muni_temp, "NO_TEST")
Exemple #25
0
def fieldMap(source, srcName, tgtName, mergeRule, outType=None):
    map = arcpy.FieldMap()
    map.addInputField(source, srcName)
    map.mergeRule = mergeRule
    outFld = map.outputField
    outFld.name = tgtName
    outFld.alias = tgtName
    outFld.aliasName = tgtName
    if outType is not None:
        outFld.type = pyTypeToIn(outType)
    map.outputField = outFld
    return map
Exemple #26
0
def CalFieldMappings(origial_shp, join_shp, nameField, fieldmappings, Length):
    fieldmappings.addTable(origial_shp)
    AddNearPoly = arcpy.FieldMap()
    AddNearPoly.addInputField(join_shp, nameField)
    field = AddNearPoly.outputField
    field.name = "NearPoly"
    field.aliasName = "NearPoly"
    field.length = Length
    AddNearPoly.mergeRule = "Join"
    AddNearPoly.joinDelimiter = ","
    AddNearPoly.outputField = field
    fieldmappings.addFieldMap(AddNearPoly)
Exemple #27
0
def createTileList():

    try:
        arcpy.management.SelectLayerByLocation(lidar, "INTERSECT", huc8,
                                               "1000 Meters", "NEW_SELECTION",
                                               "NOT_INVERT")
    finally:
        outLocation = tempfile.gettempdir()
        outName = "DEM_names.txt"

        fms = arcpy.FieldMappings()
        demName = arcpy.FieldMap()
        dirName = arcpy.FieldMap()
        demName.addInputField(lidar, "demname")
        dirName.addInputField(lidar, "dirname")
        fms.addFieldMap(demName)
        fms.addFieldMap(dirName)
        dems = arcpy.conversion.TableToTable(lidar, outLocation, outName, '',
                                             fms, '')

        return dems
Exemple #28
0
def assignFieldsByIntersect(sourceFC, assignFC, fieldsToAssign, outputFC,report_areas_overlap):
    tempWorkspace = arcpy.env.scratchGDB

    assignFields = arcpy.ListFields(dataset=assignFC)
    assignFieldsNames = [f.name for f in assignFields]

    sourceFields = arcpy.ListFields(dataset=sourceFC)
    sourceFieldNames = [f.name for f in sourceFields]
    newFields = []

    fms = arcpy.FieldMappings()
    for fieldToAssign in fieldsToAssign:
        if fieldToAssign not in assignFieldsNames:
            raise ValueError("{0} does not exist in {1}".format(fieldToAssign,assignFC))
        outputField = fieldToAssign
        if fieldToAssign in sourceFieldNames + newFields:
            outputField = Common.uniqueFieldName(fieldToAssign, sourceFieldNames + newFields)

        newFields.append(outputField)

        fm = arcpy.FieldMap()
        fm.addInputField(assignFC, fieldToAssign)
        type_name = fm.outputField
        type_name.name = outputField
        fm.outputField = type_name
        fms.addFieldMap(fm)



    fieldmappings = arcpy.FieldMappings()
    #fieldmappings.addTable(assignFC)
    #fieldmappings.removeAll()
    fieldmappings.addTable(sourceFC)
    for fm in fms.fieldMappings:
        fieldmappings.addFieldMap(fm)

    if report_areas_overlap:
        join_operation = "JOIN_ONE_TO_MANY"
    else:
        join_operation = "JOIN_ONE_TO_ONE"
    outputLayer = arcpy.SpatialJoin_analysis(target_features=sourceFC,
                                             join_features=assignFC,
                                             out_feature_class=outputFC,
                                             join_operation=join_operation,
                               join_type="KEEP_COMMON",
                              field_mapping=fieldmappings,
                              match_option="HAVE_THEIR_CENTER_IN",
                              search_radius=None,
                              distance_field_name=None)[0]


    return outputLayer
Exemple #29
0
 def pre_processing(self):
     print('Pre-processing...')
     for i, line in enumerate(self.lines):
         fields = arcpy.ListFields(line)
         field_names = []
         for field in fields:
             field_names.append(field.name)
         if self.weight not in field_names:
             arcpy.AddField_management(line, self.weight,
                                       'TEXT')  # Add the field 'weight'
         rows = arcpy.UpdateCursor(line)
         for row in rows:
             row.setValue(self.weight,
                          str(self.lines_level[i]))  # Set the weight
             rows.updateRow(row)
     self.lines.append(self.road)
     if not os.path.isfile(self.workspace_path + '\\' + 'merge_all.shp'):
         arcpy.Merge_management(self.lines, 'merge_all.shp')
     if not os.path.isfile(
             self.workspace_path + '\\' +
             'streets_polygon.shp'):  # Roads: lines to polygons
         arcpy.FeatureToPolygon_management('merge_all.shp',
                                           'streets_polygon.shp')
     if not os.path.exists(self.coverage_folder
                           ):  # Roads: poltgons to coverage to get topo
         arcpy.FeatureclassToCoverage_conversion(
             [['streets_polygon.shp', 'POLYGON']], self.coverage_folder)
     if not os.path.isfile(self.workspace_path + '\\' +
                           'arc.shp'):  # To get topo
         arcpy.FeatureClassToFeatureClass_conversion(
             self.coverage_folder + '\\arc', self.workspace_path, 'arc')
     if not os.path.isfile(self.workspace_path + '\\' +
                           self.__area_path):  # Coverage to polygons
         arcpy.FeatureClassToFeatureClass_conversion(
             self.coverage_folder + '\\polygon', self.workspace_path,
             self.__area_path)
     if not os.path.isfile(self.workspace_path + '\\' + self.__arc_path):
         # Create field mappings
         # Get weights for arc
         fld_mappings = arcpy.FieldMappings()
         fld_mappings.addTable('arc.shp')
         fld_map = arcpy.FieldMap()
         fld_map.addInputField('merge_all.shp', self.weight)
         fld_mappings.addFieldMap(fld_map)
         # Spatial join to get the weights
         arcpy.SpatialJoin_analysis('arc.shp', 'merge_all.shp',
                                    self.__arc_path, 'JOIN_ONE_TO_ONE',
                                    'KEEP_ALL', fld_mappings, 'CLOSEST')
     arcpy.Delete_management('arc.shp')
     arcpy.Delete_management('merge_all.shp')
     arcpy.Delete_management('streets_polygon.shp')
     print('Pre-processing done...')
def integrated_width(fcInLines,
                     fcInPolygons,
                     fcOutLines,
                     strMetricName="",
                     boolSegmentPolygon=False,
                     temp_workspace="in_memory"):

    fcMemLines = gis_tools.newGISDataset(temp_workspace, "lineNetwork")
    arcpy.CopyFeatures_management(fcInLines, fcMemLines)
    fieldLength = gis_tools.resetField(fcMemLines, "IW_Length", "DOUBLE")
    arcpy.CalculateField_management(fcMemLines, fieldLength, "!Shape!.length",
                                    "PYTHON")

    fcMemPolygons = gis_tools.newGISDataset(temp_workspace, 'Polygons')
    if boolSegmentPolygon:
        DividePolygonBySegment.main(fcMemLines,
                                    fcInPolygons,
                                    fcMemPolygons,
                                    temp_workspace,
                                    dblPointDensity=5.0)
    else:
        arcpy.CopyFeatures_management(fcInPolygons, fcMemPolygons)

    fieldPolygonArea = gis_tools.resetField(fcMemPolygons,
                                            strMetricName[0:6] + "Area",
                                            "DOUBLE")
    arcpy.CalculateField_management(fcMemPolygons, fieldPolygonArea,
                                    "!Shape!.area", "PYTHON")

    f_mappings = arcpy.FieldMappings()
    f_mappings.addTable(fcMemLines)
    fmap_area = arcpy.FieldMap()
    fmap_area.addInputField(fcMemPolygons, fieldPolygonArea)

    f_mappings.addFieldMap(fmap_area)

    arcpy.SpatialJoin_analysis(fcMemLines,
                               fcMemPolygons,
                               fcOutLines,
                               "JOIN_ONE_TO_ONE",
                               "KEEP_ALL",
                               field_mapping=f_mappings,
                               match_option="WITHIN")
    fieldIntegratedWidth = gis_tools.resetField(fcOutLines,
                                                "IW" + strMetricName[0:8],
                                                "DOUBLE")
    exp = "!" + fieldPolygonArea + r"! / !" + fieldLength + "!"
    print exp
    arcpy.CalculateField_management(fcOutLines, fieldIntegratedWidth, exp,
                                    "PYTHON_9.3")

    return fieldIntegratedWidth