def transformAndSwap(ZName, transName, linkFeatures, tanPlunge): minY = 0 maxY = 0 # transform if debug: addMsgAndPrint(' transforming') arcpy.TransformFeatures_edit(ZName, linkFeatures, 'SIMILARITY') testAndDelete(transName) arcpy.Copy_management(ZName, transName) # swap coordinates if debug: addMsgAndPrint(' swapping coordinates') geomType = arcpy.Describe(ZName).shapeType #addMsgAndPrint(' '+geomType if geomType == 'Point': #Point with arcpy.da.UpdateCursor( ZName, ['SHAPE@X', 'SHAPE@Y', 'SHAPE@Z']) as cursor: for row in cursor: x = row[0] y = row[1] z = row[2] newY = (z + tanPlunge * y) * cosPlunge newZ = y * cosPlunge - z * cosPlunge if newY > maxY: maxY = newY elif newY < minY: minY = newY cursor.updateRow([x, newY, newZ]) elif geomType in ('Polygon', 'Polyline'): with arcpy.da.UpdateCursor(ZName, ['SHAPE@', 'OBJECTID']) as cursor: for row in cursor: #addMsgAndPrint(str(row[0].partCount)+' parts') #addMsgAndPrint(str(row[0].pointCount)+ ' points') allParts = arcpy.Array() try: for part in row[0]: newPart = arcpy.Array() for pnt in part: if pnt: x = pnt.X y = pnt.Y z = pnt.Z newY = (z + tanPlunge * y) * cosPlunge newZ = y if newY > maxY: maxY = newY elif newY < minY: minY = newY pnt = arcpy.Point(x, newY, newZ) newPart.add(pnt) allParts.add(newPart) if geomType == 'Polygon': plg = arcpy.Polygon(allParts) else: plg = arcpy.Polyline(allParts) #addMsgAndPrint(str(plg.pointCount)+' new points') cursor.updateRow([plg, row[1]]) except: addMsgAndPrint('Problem. Skipping OBJECTID = ' + str(row[1])) return minY, maxY
#!usr/bin/python import arcpy import sys # GDBworkspace = r'E:/ArcPyscript/SZCoordinateTool/test.gdb' staticGDB = r'./static.gdb' # saveToGDB = r'./data.gdb' # inputFeatures = staticGDB + "\\" + 'Newpos' inputLinkFeatures = staticGDB + "\\" + 'LinkSZto54114' # outputBJ54 = saveToGDB + "\\" + 'result54114' # outputWGS84 = saveToGDB + "\\" + 'result' inputFeatures = arcpy.GetParameterAsText(0) outputBJ54 = arcpy.GetParameterAsText(1) outputWGS84 = arcpy.GetParameterAsText(2) Web_Mercator = r"PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]" Beijing_1954_114 = r"PROJCS['Beijing_1954_3_Degree_GK_CM_114E',GEOGCS['GCS_Beijing_1954',DATUM['D_Beijing_1954',SPHEROID['Krasovsky_1940',6378245.0,298.3]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Gauss_Kruger'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',114.0],PARAMETER['Scale_Factor',1.0],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]]" arcpy.env.overwriteOutput = True arcpy.env.qualifiedFieldNames = "UNQUALIFIED" arcpy.env.workspace = staticGDB try: arcpy.Copy_management(inputFeatures, outputBJ54) arcpy.TransformFeatures_edit(outputBJ54, inputLinkFeatures, "AFFINE") arcpy.Project_management(outputBJ54, outputWGS84, Web_Mercator, '54to84') except arcpy.ExecuteError: errorMsgs = arcpy.GetMessages(2) arcpy.AddError(str(errorMsgs)) arcpy.AddMessage("Failed!") pass
def affine_trans(entity_set1, entity_set2, text_result_file): # Compute control points with the result of textual label match. df_control_points = generate_control_points(entity_set1, entity_set2, text_result_file) # Examine whether rubber sheeting can be performed to further adjust the spatial positions of the entities. # This also means whether entities can be overlaid. overlaid = True # If the number of found control points is less than 3, overlaid will be False and the return will be original # entities. if len(df_control_points) < 3: overlaid = False entity_set1 = gpd.GeoDataFrame( pd.concat(entity_set1, ignore_index=True)) entity_set2 = gpd.GeoDataFrame( pd.concat(entity_set2, ignore_index=True)) return entity_set1, entity_set2, overlaid # If the number of found control points is greater than 3, affine transformation will be performed. In order to # remove potentially wrong found control points, this process includes three steps. # The first step is to perform an initial affine transformation. links_sour_tar(df_control_points ) # Generate map links using all found control points. folder_affine_trans( 'affine_trans1' ) # New a folder to store the result of affine transformation. trans_entity_set1 = [] # Each shapefile of the first dataset will be transformed. for i in range(len(entity_set1)): # Copy the shapefile to be transformed. locals()['gdf_' + str(i)] = entity_set1[i].copy() locals().get('gdf_' + str(i)).to_file('affine_trans1/gdf_' + str(i) + '.shp') # Perform affine transformation. arcpy.TransformFeatures_edit(in_features='affine_trans1/gdf_' + str(i) + '.shp', in_link_features="links.shp", method='AFFINE') trans_entity_set1.append( gpd.read_file('affine_trans1/gdf_' + str(i) + '.shp')) # Filter control points based on spatial distances of the control points computed with the transformed result. trans_entity_set2 = entity_set2 df_control_points_filtered = filter_cp(trans_entity_set1, trans_entity_set2, df_control_points) # Affine transformation again with filtered control points. links_sour_tar(df_control_points_filtered) folder_affine_trans('affine_trans2') trans_entity_set1 = [] for i in range(len(entity_set1)): locals()['gdf_' + str(i)] = entity_set1[i].copy() locals().get('gdf_' + str(i)).to_file('affine_trans2/gdf_' + str(i) + '.shp') arcpy.TransformFeatures_edit(in_features='affine_trans2/gdf_' + str(i) + '.shp', in_link_features="links.shp", method='AFFINE') trans_entity_set1.append( gpd.read_file('affine_trans2/gdf_' + str(i) + '.shp')) return trans_entity_set1, trans_entity_set2, overlaid