#print "Buffering all points for " + group + "\n"
         #point_buffer = arcpy.sa.EucDistance(points_fc,buffer_dist,grid_resolution)        
 
         ## get a list of the lineages in this group
         lineage_list=[]
         print "Lineages in " + group + ":"
         for row in GroupLineageList:
             if row[3] not in lineage_list:
                 if row[2] == group and "," not in row[3] and row[3] not in Lin_exclude_list:  # exclude particular lineage names and those with dodgy punctuation (fix in data later)
                     lineage_list.append(row[3])
                     print "   ", row[3]
         
         maxent_extent = maxent_raster.extent
         
         # get the extent of the points for the model group
         yrange=LineageFunctions.getFieldMinMax(points_fc,"Lat")
         ymin=yrange[0]
         ymax=yrange[1]
         xrange=LineageFunctions.getFieldMinMax(points_fc,"Long")
         xmin=xrange[0]
         xmax=xrange[1]
 
         buffer_ratio = (1 + additional_buffer)
         extent_buffer = buffer_dist * buffer_ratio
     
         # new extent is the same as points layer + a buffer
         # but where the extended buffer goes beyond the extent of the maxent model, limit to the model extent (determined by the buffered environemnt grids)
         xmin=math.floor(max([xmin - extent_buffer,maxent_extent.XMin]))
         ymin=math.floor(max([ymin - extent_buffer,maxent_extent.YMin]))
         xmax=math.ceil(min([xmax + extent_buffer,maxent_extent.XMax]))
         ymax=math.ceil(min([ymax + extent_buffer,maxent_extent.YMax]))
env.extent = Australia_extent

maxent_raster = arcpy.sa.Raster(maxent_model)

# start a list of layers to delete at the end
layers_to_delete = []

## Buffer the full set of sequenced locations   STEPS 1 & 2
print "Buffering all points\n"
point_buffer = arcpy.sa.EucDistance(points, buffer_dist, grid_resolution)
point_buffer.save("all_points_buf")  ## once the code is working, no need to save this layer
layers_to_delete.append("all_points_buf")

## from the points shapefile, get a list of the lineages
print "Lineage list\n"
lineage_list = LineageFunctions.getFieldValues(points, Lineage_field_name)
print "Lineages:"
for lineage in lineage_list:
    print "   ", lineage

# calculate the extent for weight grids
points_properties = arcpy.Describe(points)
points_extent = points_properties.extent
buffer_ratio = 1 + additional_buffer
extent_buffer = buffer_dist * buffer_ratio

# new extent is the same as points layer + a buffer
# but where the extended buffer goes beyond the extent of Australia, limit to Australia
xmin = math.floor(max([points_extent.xmin - extent_buffer, Australia_extent.XMin]))
ymin = math.floor(max([points_extent.ymin - extent_buffer, Australia_extent.YMin]))
xmax = math.ceil(min([points_extent.xmax + extent_buffer, Australia_extent.XMax]))
except:
    print "\n" + outFeatureClass + " could not be created, or already exists."
points_fc = outLocation + "/" + outFeatureClass

# Loop through the list of Model Groups
for group in GroupList:
    if group != 0 and group != "":
    #if group == "Saproscincus challengeri":   # this is a temp line to model just one group!!
    
        print "\nStarting group " + group + "\n"
        
        groupDefQuery = "[ModelGroup] = '" + group + "'"
        points_layer_lin.definitionQuery = groupDefQuery
               
        # get the extent of the points for the model group
        xrange=LineageFunctions.getFieldMinMax(points_layer_lin,"long")
        xmin=xrange[0]
        xmax=xrange[1]
        yrange=LineageFunctions.getFieldMinMax(points_layer_lin,"lat")
        ymin=yrange[0]
        ymax=yrange[1]
        env.extent = arcpy.Extent(xmin,ymin,xmax,ymax)
        
        # export a kml of the sites        
        group_nospaces = string.replace(group," ","_")
        export_lyr = points_layer_lin
        export_lyr.name = group_nospaces + "_lineages"
        export_lyr.showLabels = True
        lblClass = export_lyr.labelClasses[0]
        lblClass.expression="[Lineage]"
        kmz_name = export_lyr.name + ".kmz"