Map for each row in a shapefile with Python

663
6
05-15-2020 10:19 AM
MarcoPignatiello
New Contributor

Hi, I would like to know if it's possible to create in Python a script which for each record (row) in a shapefile (TYPE:point), the script return a map with the single point. I know how exporting maps with shapefile's points all showed, but I would like to see in each map only one point for time. I tried to do a script with definition query inside a for cycle but it didn't work!

0 Kudos
6 Replies
DavidPike
MVP Frequent Contributor
0 Kudos
MarcoPignatiello
New Contributor

Thanks for your answer. I have this shapefile and I would like to see n maps with only 1 point for each map. I try with DDT script and it renturns me n maps but with all points in each. The question is: Is it possible to do an iterete definition query based on the Field (FID)??  this field is a numerical field so I think it would be possible to do a for loop setting first FID=0, then FID=1.etc.. but I don't know how I can do it. 

Thank you for your help!

0 Kudos
DavidPike
MVP Frequent Contributor

I don't think the index field can be a number, maybe add a new field and copy the number as a string?

what does your script look like?

0 Kudos
MarcoPignatiello
New Contributor

#Set variable


shp=arcpy.mapping.Layer(r"C:\Users\Utente\Dropbox (Geomatica_DSFTA)\Admin (L)\Amiata_Via_Remedi\GIS\shp\Elementi_20200508_v2.shp")

#Enable DDP and do this for loop


for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
 mxd.dataDrivenPages.currentPageID = pageNum

#I thought with to create a loop like this but it doesnt work. For sure this is not the correct way to do it


         for a in range (1,174):
                  shp.definitionQuery='"NUM"='"a"

arcpy.mapping.ExportToPNG(mxd, r"C:\Users\Utente\Desktop\OUTPUT\map” + str(pageNum) + ".png")
del mxd

0 Kudos
DavidPike
MVP Frequent Contributor

The loop certainly looks wrong, why the 2 assignment operators? looks to be in the wrong place but can't tell the indentation.

please refer to Dan Patterson‌ 's code formatting guide /blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
MarcoPignatiello
New Contributor
mxd=arcpy.mapping.MapDocument("CURRENT")

#Set variable
shp=arcpy.mapping.Layer(r"C:\Users\Utente\Dropbox (Geomatica_DSFTA)\Admin (L)\Amiata_Via_Remedi\GIS\shp\Elementi_20200508_v2.shp")

#Enable DDP and do this for loop
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
	mxd.dataDrivenPages.currentPageID = pageNum

#I thought with to create a loop like this but it doesnt work. For sure this is not the correct way to do it
	for a in range (1,174):
		shp.definitionQuery='"NUM"='"a"
	
arcpy.mapping.ExportToPNG(mxd, r"C:\Users\Utente\Desktop\OUTPUT\map” + str(pageNum) + ".png")
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

NUM= Field of shapefile, TYPE: short integer, from 0 to 174

I assign 2 assignment operators (shp.definitionQuery='"NUM"='"a") because I want 174 maps where in the first map show only the point that corrisponding a NUM=1, then I want the same thing but NUM=2 till the end NUM=174. I dont know if its possible, I'm a beginner as a "programmer"

0 Kudos