Looping tool for creating create map tile packages works with TPKX but stops after first TPK

431
2
01-09-2024 02:32 AM
Nicole_Ueberschär
Esri Regular Contributor

I created a python script tool that is looping through sectors (administrative boundaries) to create map tile packages. It works like a charm when creating TPKX files but stops after the first feature as completed when I use TPK instead. The tool uses a search cursor to loop through the features and then query definitions to select features from other layers, then inside the search cursor instance it is creating the TPK (or TPKx). I don't see a reason why it would consider all features for TPKX but only the first for TPK. Any idea?

Tags (4)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi @Nicole_Ueberschär, can you post the script you are using?

0 Kudos
Nicole_Ueberschär
Esri Regular Contributor
import arcpy

mapname=arcpy.GetParameterAsText(0) #Map to be used
sectors=arcpy.GetParameterAsText(1) #layer to be looped
sfield=arcpy.GetParameterAsText(2) #field used for looping
layers=arcpy.GetParameter(3) #additional layers to be filtered
field=arcpy.GetParameterAsText(4) #field to be queried for
folder=arcpy.GetParameterAsText(5) #folder where packages are stored
placeholder=arcpy.GetParameterAsText(6) #prefix for package names


def main():
    #loop through sectors
    sec=arcpy.da.SearchCursor(sectors,[sfield,"SHAPE@"])
    for row in sec:
        xmin=row[1].extent.XMin
        xmax=row[1].extent.XMax
        ymin=row[1].extent.YMin
        ymax=row[1].extent.YMax
        arcpy.AddMessage(str(xmin)+" "+str(ymax)+" "+str(xmax)+" "+str(ymin))
        #left top right bottom

        p = arcpy.mp.ArcGISProject('current')
        m = p.listMaps(mapname)[0]
        s=m.listLayers(sectors)[0]
        s.definitionQuery = sfield+"='"+str(row[0])+"'"
        for layer in layers :
            l = m.listLayers(layer)[0]
            l.definitionQuery = field+"='"+row[0]+"'"

        arcpy.management.CreateMapTilePackage(
            in_map=m,
            service_type="ONLINE",
            output_file=r""+folder+"\\"+placeholder+str(row[0])+".tpk",
            format_type="JPEG",
            level_of_detail=20,
            service_file=None,
            summary="test",
            tags="test",
            extent=str(xmin)+" "+str(ymax)+" "+str(xmax)+" "+str(ymin)+' GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]',
            compression_quality=75,
            package_type="tpk",
            min_level_of_detail=14,
            area_of_interest="",
            create_multiple_packages=None,
            output_folder=None
        )


if __name__ == '__main__':
    main()

I am only changing in rows 34 and 42 from tpk to tpkx and the looping continues.

Thanks for looking into it.

Nicole

0 Kudos