##########################################################################
# Script to show use of the vector functions in BlueSprayPy
# Date: 4/12/2016
# Author: Jim Graham
##########################################################################
from BlueSprayPy import *

def WriteOutAttributesAsHTML(TheInputLayer,TheFilePath):

	TheFile=open(TheFilePath,"w")
	
	NumColumns=TheInputLayer.GetNumColumns()
	NumRows=TheInputLayer.GetNumFeatures()
	
	TheHeadings=TheInputLayer.GetHeadings()
	
	#print("NumRows="+format(NumRows)+", NumColumns="+format(NumColumns))
	
	# Start the HTML file
	
	TheFile.write("<html>\n\r")
	TheFile.write("<body>\n\r")
	
	# Start the table for the attributes
	
	TheFile.write("<table border='1' cellpadding='4' cellspacing='0'>\n\r")
	
	# Write out the header with a first column of row indexes
	
	TheFile.write("<tr>\n\r")
	TheFile.write("<td>")
	TheFile.write("Row")
	TheFile.write("</td>")
	
	Count=0
	while (Count<NumColumns):
		TheHeading=TheHeadings[Count]
		TheFile.write("<td>")
		TheFile.write(TheHeading)
		TheFile.write("</td>")
		
		#print(TheHeading)
		Count+=1
		
	TheFile.write("</tr>\n\r")
	
	# Get the columns from the table
	
	TheColumns=[]
	Count=0
	
	while (Count<NumColumns):
		Value=TheInputLayer.GetColumn(Count)
		
		TheColumns.append(Value)
		
		Count+=1
	
	# write out the rows of values
	
	Row=0
	while (Row<NumRows):
		TheFile.write("<tr>\n\r")
		
		TheFile.write("<td>")
		TheFile.write(format(Row))
		TheFile.write("</td>")
		Column=0
		while (Column<NumColumns):
			TheFile.write("<td>")
			#TheValue=GetTableCellValue(TheTable,Row,Column)
			TheValue=TheColumns[Column][Row]
			try:
				TheFile.write(format(TheValue))
			except:
				TheFile.write("Error")
			TheFile.write("</td>")
			Column+=1
			
		TheFile.write("</tr>\n\r")
	
		Row+=1
		
	TheFile.write("</table>")
	
	# Finish the HTML file
	
	TheFile.write("</body>")
	TheFile.write("</html>")
	TheFile.close()

#########################################################################
# load a large shapefile

TheInputLayer=STBlueSpray.LoadFromFile("C:/Temp/ne_110m_admin_0_countries.shp")
#TheInputLayer=STBlueSpray.LoadFromFile("C:/Temp/blrts_stps.shp")

NumFeatures=TheInputLayer.GetNumFeatures() # get the number of features from the layer
print(NumFeatures)

# Write out the layer's attributes to an HTML file

WriteOutAttributesAsHTML(TheInputLayer,"C:/Temp/Vector2.html")

# Create a new layer with just Canada

TheInputLayer.SelectFeature(27) # feature that contains Canada

TheCanadaLayer=TheInputLayer.CreateCopyFromSelected()

TheCanadaLayer.SaveToFile("C:/Temp/TheCanadaLayer.shp")

# Create a new layer with just the US

TheInputLayer.SelectFeature(168) # 

TheUSALayer=TheInputLayer.CreateCopyFromSelected()

TheUSALayer.SaveToFile("C:/Temp/TheUSALayer.shp")

# Buffer Canada

TheBufferedCanadaLayer=TheCanadaLayer.Buffer(10)

TheBufferedCanadaLayer.SaveToFile("C:/Temp/BufferedCanada.shp")

# Intersect with USA

TheUSALayer.FixProblems()

TheUSALayer.SaveToFile("C:/Temp/TheUSALayer2.shp")

TheBufferedCanadaLayer.SaveToFile("C:/Temp/TheBufferedCanadaLayer2.shp")

# Save the intersection

TheIntersectionLayer=TheUSALayer.Intersect(TheBufferedCanadaLayer)

TheIntersectionLayer.SaveToFile("C:/Temp/Intersection.shp")
