old_contents: """Create a csv matrix of distances between shapefile geometry objects.
  
  Requirements: fiona, shapely
  
  Written by: Taylor Denouden
  Date: November 25, 2015
  """
  
  import fiona
  from shapely.geometry import shape
  from multiprocessing import Pool
  
  
  def extract_ids(input_file):
      """Extract all polygon ids from input shapefile."""
      with fiona.open(input_file, 'r') as source:
          return [shp['id'] for shp in source]
  
  
  def calculate_distances(shp_id):
      """Calculate distances between `shp_id` and all other features in shapefile.
  
      `shp_id` is the feature id in the shapefile `infile` (global). `shp_id` and
      each id in `ids` (global) are cast to shapely features and then passed to a
      distance function contained in the shapely library. An array is returned
      with all these distances.
      """
      with fiona.open(infile) as source:
          source = list(source)
          result = []
  
          # Calculate distances and store in result
          i_shp = shape(source[int(shp_id)]['geometry'])
          for j in ids:
              if int(j) < int(shp_id):
                  result.append(-1)
                  continue
  
              j_shp = shape(source[int(j)]['geometry'])
              if i_shp.is_valid and j_shp.is_valid:
                  result.append(i_shp.distance(j_shp))
              else:
                  result.append(-1)
  
          return result
  
  
  if __name__ == "__main__":
      """Main execution thread."""
      infile = "./data/random_points/test_polys.shp"
      # infile = "./data/low_water_final/low_water.shp"
      ids = extract_ids(infile)
  
      print "Calculating distances"
      # Map each id to a list of distances to all other ids
      pool = Pool()  # Spawn a worker process for each cpu
      data = pool.map(calculate_distances, ids, chunksize=100)
  
      # Write the data to a new csv file
      outfile = open("test.csv", "w")
  
      # Write header
      print "Writing Header"
      outfile.write("NODE")
      for i in ids:
          outfile.write("," + i)
      outfile.write("\n")
  
      # Write rows
      print "Writing Rows"
      for i in ids:
          outfile.write(str(i) + ",")
          i = int(i)
  
          for j in ids:
              j = int(j)
              outfile.write(str(data[i][j]) + ",")
          outfile.write("\n")
  
      outfile.close()
  
new_contents: """Create a csv matrix of distances between shapefile geometry objects.
  
  Requirements: fiona, shapely
  
  Written by: Taylor Denouden
  Date: November 25, 2015
  """
  
  import fiona
  from shapely.geometry import shape
  from multiprocessing import Pool
  
  
  def extract_ids(input_file):
      """Extract all polygon ids from input shapefile."""
      with fiona.open(input_file, 'r') as source:
          return [shp['id'] for shp in source]
  
  
  def calculate_distances(shp_id):
      """Calculate distances between `shp_id` and all other features in shapefile.
  
      `shp_id` is the feature id in the shapefile `infile` (global). `shp_id` and
      each id in `ids` (global) are cast to shapely features and then passed to a
      distance function contained in the shapely library. An array is returned
      with all these distances.
      """
      with fiona.open(infile) as source:
          source = list(source)
          result = []
  
          # Calculate distances and store in result
          i_shp = shape(source[int(shp_id)]['geometry'])
          for j in ids:
              if int(j) < int(shp_id):
                  result.append(-1)
                  continue
  
              j_shp = shape(source[int(j)]['geometry'])
              if i_shp.is_valid and j_shp.is_valid:
                  result.append(i_shp.distance(j_shp))
              else:
                  result.append(-1)
  
          return result
  
  
  if __name__ == "__main__":
      """Main execution thread."""
      # infile = "./data/random_points/test_polys.shp"
      infile = "./data/low_water_final/low_water.shp"
      ids = extract_ids(infile)
  
      print "Calculating distances"
      # Map each id to a list of distances to all other ids
      pool = Pool()  # Spawn a worker process for each cpu
      data = pool.map(calculate_distances, ids, chunksize=100)
  
      # Write the data to a new csv file
      outfile = open("test.csv", "w")
  
      # Write header
      print "Writing Header"
      outfile.write("NODE")
      for i in ids:
          outfile.write("," + i)
      outfile.write("\n")
  
      # Write rows
      print "Writing Rows"
      for i in ids:
          outfile.write(str(i) + ",")
          i = int(i)
  
          for j in ids:
              j = int(j)
              outfile.write(str(data[i][j]) + ",")
          outfile.write("\n")
  
      outfile.close()
  
subject: Switch test file to actual file