# How can all polylines within a polygon be selected and then operated on to determine the average value from an attribute field?

I have two files illustrated wonderfully below: 1) Polyline (shown in red), 2) Polygon (shown in black).

The polyline file contains a numeric attribute (elevation in meters).

I would like to select all polylines that are contained within (and intersect) each polygon grid cell. I would then like to count the number of lines inside each cell, sum their elevations (from the attribute table) and report the average elevation of polylines in each cell. The lines can belong to more than one cell.

Aside from doing this manually using selections, which is not possible given the number of cells/lines, how can I accomplish this task?

In the end I would simply like a list of the average elevations for each grid cell - ideally this could all be done in the GIS, but exporting to a spreadsheet and performing the final calculations (average) is workable.

I doubt the null output could be due to this

Instead of using the SpatialJoin Geoprocessing too, you could try the below method.

Right Click your Polygon Layer > Joins and Relates - Select Join.

give the parameters as per the picture below. The counts and the sum of each attributes will be updated for each polygon grid

I would put a unique id in the polygon file. After you do your intersection, you can dissolve by the unique field and in ArcMap there is a few optional parameters where you can count the number of records that got dissolved by unique id as well as averaging the elevation values. You can then join this dissolved result back to the original polygon file if needed.

If you just need to attach the average elevation of lines falling within the polygon cell 1- assign a unique ID to each polygon 2- convert lines to point ( in this way you will got the point with elevation attribue) 3- assign polygon cell unique ID to points withing the polygon cell 4- calculate the average based on unique ID

You can accomplish this in one step using the "Spatial Join" tool.
It is available at any license level.

The key is to right-click on each join field, such as Elevation, to define the Merge Rule. These are a few of the Merge options - minimum, maximum, mean, median, sum, first, last, count.

The output will be a new Polygon layer with the aggregated polyline attributes (per cell) attached.

• I had tried Spatial Join, however I ran into the issue of receiving empty output. I tried many fixes and the only way I found to resolve it was to buffer the lines by a small amount and then perform a join on the two polygon layers. This isn't an ideal solution and I am still trying to sort out the cause of the empty output. Nov 29, 2012 at 0:22
• I had a similiar problem with Spatial Join after editing my data. After restarting ArcMap, it worked perfectly. Did you select the Polygon layer for the Target and Polyline layer for Join Features? Nov 29, 2012 at 0:35
• Yes - I also attempted some restart magic and it still failed to produce populated output. I'll clip my data and attempt it on a subset - although it's not all that big to begin with. Nov 29, 2012 at 0:55