square buffer

8091
12
Jump to solution
01-24-2015 02:40 PM
MahmoudMdookh
New Contributor II

for people who know about using python scripting in ArcMap, I'm a GIS student and I have a small project about python. I'm beginner in python language I need some help in my python script. it's about square buffer around a points feature in GIS ArcMap 10.1? and I have tried all my best to do this but it's not working .. Hope you'll help me If you don't know "Just tag this post to anyone who know about this" Thanks in advance.

This is my script:

    import arcpy
    import math
    import os
    from arcpy import env
    env
.overwriteOutput=False

    #Point Feature Class that the square buffers will be created around
    Input_Points= arcpy.GetParameterAsText(0)
    #Path and name for Output Polygon Feature Class
    out_Feature_Class
=arcpy.GetParameterAsText(1)
    Out_Path=os.path.dirname(out_Feature_Class)
    out_Name
=os.path.basename(out_Feature_Class)
    #Height
    h
= arcpy.GetParameterAsText(2)
    h
=float(h)
    #Width
    w
= arcpy.GetParameterAsText(3)
    w
=float(w)
    cursor
=arcpy.da.SearchCursor(Input_Points,"Shape")
    fields
=cursor.fields
    for field in fields:
      m
=cursor.next()
      x
=m[0][0]
      y
=m[0][1]
      x1
=x-(w/2)
      x2
=x+(w/2)
      x3
=x+(w/2)
      x4
=x-(w/2)
      y1
=y-(h/2)
      y2
=y-(h/2)
      y3
=y-(h/2)
      y4
=y-(h/2)
      pnt1
=arcpy.Point(x1,y1)
      pnt2
=arcpy.Point(x2,y2)
      pnt3
=arcpy.Point(x3,y3)
      pnt4
=arcpy.Point(x4,y4)
      arr
=arcpy.Array([pnt1,pnt2,pnt3,pnt4,pnt1])
      square_buffer
=arcpy.Polygon(arr)

    arcpy
.CopyFeatures_management(square_buffer,"square_buffer")

    arcpy
.CreateFeatureclass_management(Out_Path,out_Name,"POINT")
    arcpy
.AddField_management(out_Feature_Class,"x","FLOAT")
    arcpy
.AddField_management(out_Feature_Class,"y","FLOAT")
    arcpy
.AddField_management(out_Feature_Class,"POI","TEXT")
    arcpy
.AddField_management(out_Feature_Class,"Height","DOUBLE")
    arcpy
.AddField_management(out_Feature_Class,"Width","DOUBLE")

    with arcpy.da.InsertCursor(out_Feature_Class,("SHAPE@xy","x","y","POI","Height","Width"))as cursor:
     
for line in cursor:
      attributes
=line.split(",")
      x
=attributes[0]
      x
=float(x)
      y
=attributes[1]
      y
=float(y)
      POI
=attributes[3]
     
Height=attributes[4]
     
Width=attributes[4]
      row
=((x,y),x,y,POI,Height,Width)
     
#arcpy.Add massege(row)
      cursor
.insertRow(row)

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

In addition a few comments on your code:

  • to split the path from the file name you can use os.path.split
  • it is better to use a "with" statement for you cursor, this will clear it from memory after the cursor finishes
  • when you create the polygon, it is better to provide the spatial reference from the input points, otherwise you will be creating a featureclass without coordinate system
  • After the cursor you first copy the square buffers into a new featureclass for which you do not specify the path. Since you have not set the arcpy.env.workspace you are not guiding where it will be created
  • Next you create an empty featureclass and start an insert cursor on it. The point is that you are looping through the features in an empty featureclass which has 0 features,,,
  • In case it would have been a valid cursor with features, the index to the height fields would be wrong (is 5, not 4).

Some more tips and examples can be found here: Some Python Snippets

View solution in original post

12 Replies
XanderBakker
Esri Esteemed Contributor

You can find a script that does this here: Re: Hello everybody I need some help about python scripting to create a square buffer around points ...

Another suggestion; When posting code please use syntax highlighting. Your can read how to do this here:

Posting Code blocks in the new GeoNet

XanderBakker
Esri Esteemed Contributor

In addition a few comments on your code:

  • to split the path from the file name you can use os.path.split
  • it is better to use a "with" statement for you cursor, this will clear it from memory after the cursor finishes
  • when you create the polygon, it is better to provide the spatial reference from the input points, otherwise you will be creating a featureclass without coordinate system
  • After the cursor you first copy the square buffers into a new featureclass for which you do not specify the path. Since you have not set the arcpy.env.workspace you are not guiding where it will be created
  • Next you create an empty featureclass and start an insert cursor on it. The point is that you are looping through the features in an empty featureclass which has 0 features,,,
  • In case it would have been a valid cursor with features, the index to the height fields would be wrong (is 5, not 4).

Some more tips and examples can be found here: Some Python Snippets

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

Xander Bakker wrote:

In addition a few comments on your code:

...

  • it is better to use a "with" statement for you cursor, this will clear it from memory after the cursor finishes

...

As much as I have advocated for using with statements with ArcPy Data Access (arcpy.da) cursors, I have come to find those cursors don't quite work as advertised when using with statements.  I agree the ArcGIS Help/Documentation strongly implies what you state is how the software works:

Search Cursor - Data Access module:

Search cursors can be iterated using a For loop. Search cursors also support With statements; using a With statement will guarantee close and release of database locks and reset iteration.

Closing and releasing database locks are guaranteed!!  Those are pretty strong words.  Unfortunately, they oversimplify the situation enough that I argue they wrong.  I haven't gone through every type of data source, but I do know that using with statements on shapefiles leaves one lock behind while feature classes in file geodatabases leave two locks behind.  The locks can be removed by explicitly deleting the cursor object, but the documentation doesn't necessarily imply that and none of the examples in the documentation show it.

I have submitted a bug to Esri to either correct the cursors or update the documentation.  Of course, I don't hold my breath on them doing anything about it because any end-user confusion doesn't cost them anything, you already bought the software!

BUG-000083762:

In each cursor documentation, specify the type of lock being closed and released, as a shared lock is still present in the geodatabase after the 'with' statement executes.

XanderBakker
Esri Esteemed Contributor

Thanx Joshua Bixby‌ for the addition.

I think I have seen your comment before. Good to know. So delete the cursor objects for now, but (believing that the issue will be resolved) the with statement will be the way to go (in a near future)...

BlakeTerhune
MVP Regular Contributor

I was intrigued by your post and stumbled across this thread. In it, Lucas Danzinger of Esri found that if the with statement was run inside a function that the locks were released correctly. He also logged this under Bug NIM-089529.

Re: Python WITH statement does not release da cursor schema locks

I have not tested this yet.

BlakeTerhune
MVP Regular Contributor

UPDATE:

I am on Win7 64-bit with ArcGIS 10.2.2 using PyScripter for Python 2.7.

I can confirm that running the with statement inside of a function like this will remove the locks in a file geodatabase when done (did not test shapefile).

import arcpy

def main():

    fc = r"C:\TestData\TEMP.gdb\TEMP_FC"

    fields = ["*"]

    with arcpy.da.SearchCursor(fc, fields) as cursor:

        for row in cursor:

            print(row)

if __name__ == '__main__':

    main()

If you run the with statement outside of a function, the locks remain after the script has completed. I even tried using arcpy.ClearWorkspaceCache_management()

import arcpy

fc = r"C:\TestData\TEMP.gdb\TEMP_FC

fields = ["*"] 

with arcpy.da.SearchCursor(fc, fields) as cursor: 

    for row in cursor: 

        print(row)

However, I also noticed that the locks go away once you close PyScripter! Same thing happens if you just run the .py file with python.exe, the locks go away when the script is finished running. So this is really only an issue when running code inside if the IDE. Either way, it's probably good practice to have your code in a function anyway.

0 Kudos
MahmoudMdookh
New Contributor II

thank u so much Mr. Xander Bakker

after my respect , could u solve my problem and sent it to me "if you have free time "

I'll appreciate

0 Kudos
XanderBakker
Esri Esteemed Contributor

Did you look at this post: Re: Hello everybody I need some help about python scripting to create a square buffer around points ...  It contains the code you are looking for. You only need to include the parameters so you can use it as a Tool...

DanPatterson_Retired
MVP Esteemed Contributor

Error messages?  if so note them...

Have you search on this forum for 'square buffer' and if so, why did you rule out this thread.

And to help out readers, next time turn on the Use advanced editor option, highlight your code, click on the >> select syntax highlighting, and select Python, It provides a better more familiar view to readers.

Also...you do the tagging, at least you placed the thread in an appropriate location.