Creating new field in shapefile using ArcPy? The Next CEO of Stack OverflowCalculating field...

Inappropriate reference requests from Journal reviewers

How to solve a differential equation with a term to a power?

Which tube will fit a -(700 x 25c) wheel?

Make solar eclipses exceedingly rare, but still have new moons

Complex fractions

Can I equip Skullclamp on a creature I am sacrificing?

Non-deterministic sum of floats

Why am I allowed to create multiple unique pointers from a single object?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

Different harmonic changes implied by a simple descending scale

If the heap is initialized for security, then why is the stack uninitialized?

Why does standard notation not preserve intervals (visually)

How to avoid supervisors with prejudiced views?

Why didn't Khan get resurrected in the Genesis Explosion?

What is ( CFMCC ) on ILS approach chart?

Is "for causing autism in X" grammatical?

Is HostGator storing my password in plaintext?

Rotate a column

MessageLevel in QGIS3

In excess I'm lethal

Unreliable Magic - Is it worth it?

Is there an analogue of projective spaces for proper schemes?

Can you replace a racial trait cantrip when leveling up?

Written every which way



Creating new field in shapefile using ArcPy?



The Next CEO of Stack OverflowCalculating field in ArcGIS Desktop using ArcPy cursor?Renaming field using ArcPy?Where clause problems when all parts are user input variablesTrying to preserve attributes using SearchCursorCreating Multiple Field Indexes Using ArcpyJoining table to shapefile without creating new shapefile in ArcPy?Copying exact field properties to new layer using ArcPy?ArcPy Cursor does not fill new field?Performing field calculation on new field using partial text from existing field via selection using ArcPy?Finding difference between populations from 1990 to 2010 and writing info to new field using ArcPy?












1















I am new to scripting and to Python/arcpy. My intention with this script is to:




  • select points from a featureclass that fall within a target polygon and that meet an attribute query specified by the 'amenities' variable.

  • create three new featureclasses of these points.

  • add a new textfield to each of the featureclasses.

  • update that field.


I am stuck at the step where I would add the new field to each new feature class and update the cursor.



#Determine area of interst and make seperate shape files of amenities of interest
import arcpy
import os

arcpy.env.overwriteOutput = True

#Ammenities of interest
amenities = ['school', 'hospital', 'place of worship']
#Country of interest
targetCountry = 'El Salvador'
#Source of Data
dataSource = "Open Source"
#Central America shape file that is needed to find EL Salvador
countries = "C:\GEOG485\Lesson3\Lesson3Data\CentralAmerica.shp"
#All amenities are located here
points = "C:\GEOG485\Lesson3\Lesson3Data\OSMpoints.shp"
#Output folder
targetPoints = "C:\GEOG485\Lesson3\Lesson3Data\Output"

arcpy.env.workspace = "C:\GEOG485\Lesson3"
#Isolate the country polygon of interest
try:
countryQuery = '"NAME" = ' + "'" + targetCountry + "'"
print countryQuery

arcpy.MakeFeatureLayer_management(countries, "targetCountryLayer", countryQuery)
print arcpy.Exists("targetCountryLayer")

except:
print ("Error, try again")

try:
for amenity in amenities:
amenitySelectionClause = '"amenity" = ' + "'" + amenity + "'"
#Make a feature layer for the amenities of interest
arcpy.MakeFeatureLayer_management(points, "pointLayer", amenitySelectionClause)
#Narrow the amenities of interest to just inside the selected country
arcpy.SelectLayerByLocation_management("pointLayer", "CONTAINED_BY", "targetCountryLayer")
#Seperate shape file for each point of interest
targetShp = os.path.join(targetPoints, "{}.shp".format(amenity))
arcpy.CopyFeatures_management("pointLayer", targetShp)
#Verify the field name that will be added to the attribute table and string size
fieldName = arcpy.ValidateFieldName("Source")
arcpy.AddField_management(targetShp, fieldName, "Text", "", "", 25)
#Add row and enter text of where the data came from
with arcpy.da.UpdateCursor(targetShp, fieldName) as cursor:
for row in cursor:
row = dataSource
cursor.updateRow(row)
print ("Sucess")

except:
print ("Error, try again")

arcpy.Delete_management("targetCountryLayer")
arcpy.Delete_management("pointLayer")
print ("cleanup")









share|improve this question









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    cursor.updateRow() takes a sequence, but you're feeding in a single value. Try cursor.updateRow([dataSource]). If it doesn't work after that, we'll need the actual error message which you're currently suppressing with the try/except block. It's generally bad practice to use "bare" try/except blocks since that hides whatever actual error is occurring whether or not it's one you might expect, and those tracebacks are essential for debugging.

    – mikewatt
    1 hour ago


















1















I am new to scripting and to Python/arcpy. My intention with this script is to:




  • select points from a featureclass that fall within a target polygon and that meet an attribute query specified by the 'amenities' variable.

  • create three new featureclasses of these points.

  • add a new textfield to each of the featureclasses.

  • update that field.


I am stuck at the step where I would add the new field to each new feature class and update the cursor.



#Determine area of interst and make seperate shape files of amenities of interest
import arcpy
import os

arcpy.env.overwriteOutput = True

#Ammenities of interest
amenities = ['school', 'hospital', 'place of worship']
#Country of interest
targetCountry = 'El Salvador'
#Source of Data
dataSource = "Open Source"
#Central America shape file that is needed to find EL Salvador
countries = "C:\GEOG485\Lesson3\Lesson3Data\CentralAmerica.shp"
#All amenities are located here
points = "C:\GEOG485\Lesson3\Lesson3Data\OSMpoints.shp"
#Output folder
targetPoints = "C:\GEOG485\Lesson3\Lesson3Data\Output"

arcpy.env.workspace = "C:\GEOG485\Lesson3"
#Isolate the country polygon of interest
try:
countryQuery = '"NAME" = ' + "'" + targetCountry + "'"
print countryQuery

arcpy.MakeFeatureLayer_management(countries, "targetCountryLayer", countryQuery)
print arcpy.Exists("targetCountryLayer")

except:
print ("Error, try again")

try:
for amenity in amenities:
amenitySelectionClause = '"amenity" = ' + "'" + amenity + "'"
#Make a feature layer for the amenities of interest
arcpy.MakeFeatureLayer_management(points, "pointLayer", amenitySelectionClause)
#Narrow the amenities of interest to just inside the selected country
arcpy.SelectLayerByLocation_management("pointLayer", "CONTAINED_BY", "targetCountryLayer")
#Seperate shape file for each point of interest
targetShp = os.path.join(targetPoints, "{}.shp".format(amenity))
arcpy.CopyFeatures_management("pointLayer", targetShp)
#Verify the field name that will be added to the attribute table and string size
fieldName = arcpy.ValidateFieldName("Source")
arcpy.AddField_management(targetShp, fieldName, "Text", "", "", 25)
#Add row and enter text of where the data came from
with arcpy.da.UpdateCursor(targetShp, fieldName) as cursor:
for row in cursor:
row = dataSource
cursor.updateRow(row)
print ("Sucess")

except:
print ("Error, try again")

arcpy.Delete_management("targetCountryLayer")
arcpy.Delete_management("pointLayer")
print ("cleanup")









share|improve this question









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    cursor.updateRow() takes a sequence, but you're feeding in a single value. Try cursor.updateRow([dataSource]). If it doesn't work after that, we'll need the actual error message which you're currently suppressing with the try/except block. It's generally bad practice to use "bare" try/except blocks since that hides whatever actual error is occurring whether or not it's one you might expect, and those tracebacks are essential for debugging.

    – mikewatt
    1 hour ago
















1












1








1








I am new to scripting and to Python/arcpy. My intention with this script is to:




  • select points from a featureclass that fall within a target polygon and that meet an attribute query specified by the 'amenities' variable.

  • create three new featureclasses of these points.

  • add a new textfield to each of the featureclasses.

  • update that field.


I am stuck at the step where I would add the new field to each new feature class and update the cursor.



#Determine area of interst and make seperate shape files of amenities of interest
import arcpy
import os

arcpy.env.overwriteOutput = True

#Ammenities of interest
amenities = ['school', 'hospital', 'place of worship']
#Country of interest
targetCountry = 'El Salvador'
#Source of Data
dataSource = "Open Source"
#Central America shape file that is needed to find EL Salvador
countries = "C:\GEOG485\Lesson3\Lesson3Data\CentralAmerica.shp"
#All amenities are located here
points = "C:\GEOG485\Lesson3\Lesson3Data\OSMpoints.shp"
#Output folder
targetPoints = "C:\GEOG485\Lesson3\Lesson3Data\Output"

arcpy.env.workspace = "C:\GEOG485\Lesson3"
#Isolate the country polygon of interest
try:
countryQuery = '"NAME" = ' + "'" + targetCountry + "'"
print countryQuery

arcpy.MakeFeatureLayer_management(countries, "targetCountryLayer", countryQuery)
print arcpy.Exists("targetCountryLayer")

except:
print ("Error, try again")

try:
for amenity in amenities:
amenitySelectionClause = '"amenity" = ' + "'" + amenity + "'"
#Make a feature layer for the amenities of interest
arcpy.MakeFeatureLayer_management(points, "pointLayer", amenitySelectionClause)
#Narrow the amenities of interest to just inside the selected country
arcpy.SelectLayerByLocation_management("pointLayer", "CONTAINED_BY", "targetCountryLayer")
#Seperate shape file for each point of interest
targetShp = os.path.join(targetPoints, "{}.shp".format(amenity))
arcpy.CopyFeatures_management("pointLayer", targetShp)
#Verify the field name that will be added to the attribute table and string size
fieldName = arcpy.ValidateFieldName("Source")
arcpy.AddField_management(targetShp, fieldName, "Text", "", "", 25)
#Add row and enter text of where the data came from
with arcpy.da.UpdateCursor(targetShp, fieldName) as cursor:
for row in cursor:
row = dataSource
cursor.updateRow(row)
print ("Sucess")

except:
print ("Error, try again")

arcpy.Delete_management("targetCountryLayer")
arcpy.Delete_management("pointLayer")
print ("cleanup")









share|improve this question









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am new to scripting and to Python/arcpy. My intention with this script is to:




  • select points from a featureclass that fall within a target polygon and that meet an attribute query specified by the 'amenities' variable.

  • create three new featureclasses of these points.

  • add a new textfield to each of the featureclasses.

  • update that field.


I am stuck at the step where I would add the new field to each new feature class and update the cursor.



#Determine area of interst and make seperate shape files of amenities of interest
import arcpy
import os

arcpy.env.overwriteOutput = True

#Ammenities of interest
amenities = ['school', 'hospital', 'place of worship']
#Country of interest
targetCountry = 'El Salvador'
#Source of Data
dataSource = "Open Source"
#Central America shape file that is needed to find EL Salvador
countries = "C:\GEOG485\Lesson3\Lesson3Data\CentralAmerica.shp"
#All amenities are located here
points = "C:\GEOG485\Lesson3\Lesson3Data\OSMpoints.shp"
#Output folder
targetPoints = "C:\GEOG485\Lesson3\Lesson3Data\Output"

arcpy.env.workspace = "C:\GEOG485\Lesson3"
#Isolate the country polygon of interest
try:
countryQuery = '"NAME" = ' + "'" + targetCountry + "'"
print countryQuery

arcpy.MakeFeatureLayer_management(countries, "targetCountryLayer", countryQuery)
print arcpy.Exists("targetCountryLayer")

except:
print ("Error, try again")

try:
for amenity in amenities:
amenitySelectionClause = '"amenity" = ' + "'" + amenity + "'"
#Make a feature layer for the amenities of interest
arcpy.MakeFeatureLayer_management(points, "pointLayer", amenitySelectionClause)
#Narrow the amenities of interest to just inside the selected country
arcpy.SelectLayerByLocation_management("pointLayer", "CONTAINED_BY", "targetCountryLayer")
#Seperate shape file for each point of interest
targetShp = os.path.join(targetPoints, "{}.shp".format(amenity))
arcpy.CopyFeatures_management("pointLayer", targetShp)
#Verify the field name that will be added to the attribute table and string size
fieldName = arcpy.ValidateFieldName("Source")
arcpy.AddField_management(targetShp, fieldName, "Text", "", "", 25)
#Add row and enter text of where the data came from
with arcpy.da.UpdateCursor(targetShp, fieldName) as cursor:
for row in cursor:
row = dataSource
cursor.updateRow(row)
print ("Sucess")

except:
print ("Error, try again")

arcpy.Delete_management("targetCountryLayer")
arcpy.Delete_management("pointLayer")
print ("cleanup")






arcpy fields-attributes cursor






share|improve this question









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 17 mins ago









PolyGeo

53.8k1781245




53.8k1781245






New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









WillWill

61




61




New contributor




Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Will is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    cursor.updateRow() takes a sequence, but you're feeding in a single value. Try cursor.updateRow([dataSource]). If it doesn't work after that, we'll need the actual error message which you're currently suppressing with the try/except block. It's generally bad practice to use "bare" try/except blocks since that hides whatever actual error is occurring whether or not it's one you might expect, and those tracebacks are essential for debugging.

    – mikewatt
    1 hour ago
















  • 1





    cursor.updateRow() takes a sequence, but you're feeding in a single value. Try cursor.updateRow([dataSource]). If it doesn't work after that, we'll need the actual error message which you're currently suppressing with the try/except block. It's generally bad practice to use "bare" try/except blocks since that hides whatever actual error is occurring whether or not it's one you might expect, and those tracebacks are essential for debugging.

    – mikewatt
    1 hour ago










1




1





cursor.updateRow() takes a sequence, but you're feeding in a single value. Try cursor.updateRow([dataSource]). If it doesn't work after that, we'll need the actual error message which you're currently suppressing with the try/except block. It's generally bad practice to use "bare" try/except blocks since that hides whatever actual error is occurring whether or not it's one you might expect, and those tracebacks are essential for debugging.

– mikewatt
1 hour ago







cursor.updateRow() takes a sequence, but you're feeding in a single value. Try cursor.updateRow([dataSource]). If it doesn't work after that, we'll need the actual error message which you're currently suppressing with the try/except block. It's generally bad practice to use "bare" try/except blocks since that hides whatever actual error is occurring whether or not it's one you might expect, and those tracebacks are essential for debugging.

– mikewatt
1 hour ago












0






active

oldest

votes












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Will is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f317210%2fcreating-new-field-in-shapefile-using-arcpy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Will is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Will is a new contributor. Be nice, and check out our Code of Conduct.













Will is a new contributor. Be nice, and check out our Code of Conduct.












Will is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f317210%2fcreating-new-field-in-shapefile-using-arcpy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Щит и меч (фильм) Содержание Названия серий | Сюжет |...

is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

Meter-Bus Содержание Параметры шины | Стандартизация |...