Select to view content in your preferred language

creating a polygon (footprint) with 4 coordinates from excel file

5007
9
Jump to solution
11-25-2016 07:45 AM
NikolaiKirch
Emerging Contributor

So i got ArcMap Basic and i want to bring my 100000 rows thick excel table to GIS.

In overall 8 columns are my 4 coordinates: coordinate 1 lat, coordinate 1 long, coordinate 2 lat .... 

would be easy to average the 4 coordinates and just show for every row one point.

But i need to present every row as a polygon. A polygon square with 4 vertex (one for every coordinate). Like a footprint.

Does anyone have an idea?

1 Solution

Accepted Solutions
FC_Basson
MVP Regular Contributor

Here's a basic version of a script that might work for you:

import arcpy, os, csv, numpy as np
csvfile = r'D:\GIS\Scripts\points.csv'
arcpy.env.workspace = r'D:\GIS\ArcGIS\scratch.gdb'
polygons = arcpy.mapping.Layer(r'D:\GIS\ArcGIS\scratch.gdb\csv_points_poly')
csv = np.genfromtxt(csvfile, delimiter=",")
srWGS = arcpy.SpatialReference(4326)
# from second line of array
cur = arcpy.da.InsertCursor(polygons, ("ID", "SHAPE@"))
for coord in csv[1:]:
 print coord
 array = arcpy.Array()
 # point 1
 pnt1 = arcpy.PointGeometry(arcpy.Point(coord[2],coord[1]),srWGS,False, False)
 array.append(pnt1.centroid)
 # point 2
 pnt2 = arcpy.PointGeometry(arcpy.Point(coord[4],coord[3]),srWGS,False, False)
 array.append(pnt2.centroid)
 # point 3
 pnt3 = arcpy.PointGeometry(arcpy.Point(coord[6],coord[5]),srWGS,False, False)
 array.append(pnt3.centroid)
 # point 4
 pnt4 = arcpy.PointGeometry(arcpy.Point(coord[8],coord[7]),srWGS,False, False)
 array.append(pnt4.centroid)
 polygon = arcpy.Polygon(array,srWGS)
 cur.insertRow((coord[0],polygon))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

First I created the output polygon feature class "csv_points_poly" in my scratch GDB with an additional "ID" field.

Then assuming your XLS/CSV file looks something like this:

ID,LAT1,LON1,LAT2,LON2,LAT3,LON3,LAT4,LON4
1,-34.5,17,-34.5,17.5,-35,17.5,-35,17
2,-34,18,-34,18.5,-34.5,18.5,-34.5,18
3,-33.5,19,-33.5,19.5,-34,19.5,-34,19
4,-32.5,20,-32.5,20.5,-33,20.5,-33,20
5,-31.5,21,-31.5,21.5,-32,21.5,-32,21
6,-30.5,22,-30.5,22.5,-31,22.5,-31,22
7,-30,23,-30,23.5,-30.5,23.5,-30.5,23
8,-29,24,-29,24.5,-29.5,24.5,-29.5,24
9,-28,25,-28,25.5,-28.5,25.5,-28.5,25
10,-27,26,-27,26.5,-27.5,26.5,-27.5,26‍‍‍‍‍‍‍‍‍‍‍

You can use numpy to make an array of your data, which you can iterate to build a polygon for each row.

View solution in original post

9 Replies
DanPatterson_Retired
MVP Emeritus

The obvious choice is Create Fishnet—Help | ArcGIS for Desktop if your polygon pattern is regular and just a bunch of sequential rectangles.  

If not, they you might have to provide some more information and give some sense on your familiarity with python 

NikolaiKirch
Emerging Contributor

Its not regular.

for example these are the coordinates from one row for one polygon:

coordinate 1 lat  coordinate 1 long coordinate 2 lat

50,48508401 6,98761143 49,672045846,78847988849,628083627,19830872650,441042317,404422742

i need to create a heatmap with these polygons.

My python skills are quite rudimentary.

0 Kudos
NeilAyres
MVP Alum

Well the best way is to write some python to ingest this and create a polygon featureclass from the file.

Its not too difficult.

I imagine that this is actually a text file, rather than xls or something.

Is that your decimal separator ","?

If you can post a portion of the file here, I am sure someone can take a look.

FC_Basson
MVP Regular Contributor

Here's a basic version of a script that might work for you:

import arcpy, os, csv, numpy as np
csvfile = r'D:\GIS\Scripts\points.csv'
arcpy.env.workspace = r'D:\GIS\ArcGIS\scratch.gdb'
polygons = arcpy.mapping.Layer(r'D:\GIS\ArcGIS\scratch.gdb\csv_points_poly')
csv = np.genfromtxt(csvfile, delimiter=",")
srWGS = arcpy.SpatialReference(4326)
# from second line of array
cur = arcpy.da.InsertCursor(polygons, ("ID", "SHAPE@"))
for coord in csv[1:]:
 print coord
 array = arcpy.Array()
 # point 1
 pnt1 = arcpy.PointGeometry(arcpy.Point(coord[2],coord[1]),srWGS,False, False)
 array.append(pnt1.centroid)
 # point 2
 pnt2 = arcpy.PointGeometry(arcpy.Point(coord[4],coord[3]),srWGS,False, False)
 array.append(pnt2.centroid)
 # point 3
 pnt3 = arcpy.PointGeometry(arcpy.Point(coord[6],coord[5]),srWGS,False, False)
 array.append(pnt3.centroid)
 # point 4
 pnt4 = arcpy.PointGeometry(arcpy.Point(coord[8],coord[7]),srWGS,False, False)
 array.append(pnt4.centroid)
 polygon = arcpy.Polygon(array,srWGS)
 cur.insertRow((coord[0],polygon))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

First I created the output polygon feature class "csv_points_poly" in my scratch GDB with an additional "ID" field.

Then assuming your XLS/CSV file looks something like this:

ID,LAT1,LON1,LAT2,LON2,LAT3,LON3,LAT4,LON4
1,-34.5,17,-34.5,17.5,-35,17.5,-35,17
2,-34,18,-34,18.5,-34.5,18.5,-34.5,18
3,-33.5,19,-33.5,19.5,-34,19.5,-34,19
4,-32.5,20,-32.5,20.5,-33,20.5,-33,20
5,-31.5,21,-31.5,21.5,-32,21.5,-32,21
6,-30.5,22,-30.5,22.5,-31,22.5,-31,22
7,-30,23,-30,23.5,-30.5,23.5,-30.5,23
8,-29,24,-29,24.5,-29.5,24.5,-29.5,24
9,-28,25,-28,25.5,-28.5,25.5,-28.5,25
10,-27,26,-27,26.5,-27.5,26.5,-27.5,26‍‍‍‍‍‍‍‍‍‍‍

You can use numpy to make an array of your data, which you can iterate to build a polygon for each row.

NikolaiKirch
Emerging Contributor

i changed the paths for my directories, created the feature class with an additional ID field and saved my xls as csv just like your table. My (german) csv seems to be seperated trough ";", i changed

csv = np.genfromtxt(csvfile, delimiter=",") to ";"

otherwise the script wont work.

Unfortunately the whole result is like:

[ 7899.    nan    nan    nan    nan    nan    nan    nan    nan]
[ 7900.    nan    nan    nan    nan    nan    nan    nan    nan]
[ 7901.    nan    nan    nan    nan    nan    nan    nan    nan]

with some:

[ 26710.     nan     nan     nan     nan     nan     nan     nan     nan]
[  2.67110000e+04   4.70000000e+01   7.00000000e+00   4.60000000e+01
   7.00000000e+00   4.60000000e+01   8.00000000e+00   4.70000000e+01
   8.00000000e+00]
[ 26712.     nan     nan     nan     nan     nan     nan     nan     nan]

is it possible that my my lat or lon coordinates are too big for "double"?

they look like that:

54,2506883777232
0 Kudos
FC_Basson
MVP Regular Contributor

No I don't think your coordinate float value is "too big", but make sure that the float is not expressed as a comma value (54,2506883777232), but with dot notation (54.2506883777232).  Revisit your input file's values to ensure that the field formatting is correct..

NeilAyres
MVP Alum

You will have to go into your windows regional settings and change the decimal separator from "," to "."

NikolaiKirch
Emerging Contributor

it works fine, but it seems to me that lat and long are reversed?

0 Kudos
FC_Basson
MVP Regular Contributor

Oh yes, for creating a point geometry the notation is Lon/Lat (Writing geometries—ArcPy Get Started | ArcGIS for Desktop ) and not the format I used in my script.  So just change the sequence of the coordinate pairs to match the LAT and LON fields (see edits to my original script code):

pnt1 = arcpy.PointGeometry(arcpy.Point(coord[2],coord[1]),srWGS,False, False)‍‍