def get_matching_polygon(rHealpix_cell, shapeFile_path):
    # read in the file
    r = shapefile.Reader(shapeFile_path)
    fields = r.fields[1:]
    # for item in r.fields:
    #     print(item)  # prints the fields in the shapefile??

    # get the attribute table records (combined with shapes) ie shapeRecords
    shapeRecs = r.shapeRecords()

    print(rHealpix_cell)

    #find centroid
    # make an dggs instance
    rdggs = RHEALPixDGGS()

    thisDGGS = rHealpix_cell
    # print(thisDGGS)
    # print(' ')
    # convert to proper format
    dggsLoc = list()  # empty list ready to fill
    for item in thisDGGS:  # build a dggs location cell as a list like dggsLoc = ['R', 7, 2, 4, 5, 6, 3, 2, 3, 4, 3, 8, 3]
        if item.isalpha():  # the letter 'R' at the beginning
            dggsLoc.append(item)
        else:
            item = int(item)  # the numbers in the cell
            dggsLoc.append(item)

    c = Cell(rdggs, dggsLoc)
    centroid = c.centroid(plane=False)
    nucleus = c.nucleus()  #seems to be in meters

    print('nucleus', nucleus[0],
          nucleus[1])  #nucleus seems to be in metres - need it in Lat Long
    print('centroid', centroid[0], centroid[1])
    dggsPoint = Point(centroid[0], centroid[1])

    #findout which poly the centroid is in
    # use shapely

    #for spatial shape in shapefile
    for item in shapeRecs:
        thisItem = item.shape
        # define the shape and give it to shapely
        thisShp = thisItem.points  # this gives a list of points the function input poly has in it
        # now convert to a shapely Polygon
        thisPoly = Polygon(
            thisShp)  # thisPoly is the Shapely version of the poly

        if dggsPoint.within(thisPoly):  # take the point from DGGS cell nucleus
            print('Found SA1', item.record[0])
            return item.record[0]
Example #2
0
#set up shapefile for output
w = shapefile.Writer(shapefile.POINT)

w.field('DGGSrHEALPix', 'C', '20')
w.field('LongiWGS84', 'C',
        '20')  #using 'C' = character = ensures the correct number
w.field('LatiWGS84', 'C', '20')
w.field('Approx_width', 'C', '20')
w.field('DGGS_reso', 'C', '20')
w.field('LGAcode', 'C', '20')
w.field('Name', 'C', '20')
w.field('dggs_cell', 'C', '100')

# make an dggs instance
rdggs = RHEALPixDGGS()

# open a shape file to get your polygons from
myFile = r'\\xxxxxxxxxx\LGA_2018_no_multipart.shp'  #LGS's can be downloaded from ABS?

# read in the file
r = shapefile.Reader(myFile)

# get the attribute table records (combined with shapes) ie shapeRecords
shapeRecs = r.shapeRecords()


# function to write a list to a csv file
# requires the list and the filename to save it too
def write_list_to_file(myList, filename):
    """Write the list to csv file."""
Example #3
0
The code finds the DGGS cell for each location (level 12?)

'''

f = r'\\xxxxxxx\RiverGauges\RiverGaugeLocationsAU.csv'

output = list()

import csv
from dggs import RHEALPixDGGS

import math
from utils import my_round

# make an instance
rdggs = RHEALPixDGGS()


# function to write a list to a csv file
# requires the list and the filename to save it too
def write_list_to_file(myList, filename):
    """Write the list to csv file."""

    with open(filename, "w") as outfile:
        for entries in myList:
            outfile.write(entries)
            # add a return after each line
            outfile.write("\n")


def cleanPosition(pos):
Example #4
0
This code calculates all the rHealPix cells in a polygon at the resolution asked for.
It will calculate the DGGS cells and return them to the code.
This example is based on ABS Local Government Areas shapefile.
ESRI not required but it can read the shapefile outputs

Joseph Bell Geoscience Australia

'''

from dggs import RHEALPixDGGS

from shapely.geometry import shape, Point, Polygon  # used in the function below
# import csv

# make an instance
rdggs = RHEALPixDGGS()

# a function to calculate DGGS cells within a polygon
# mypoly is the shape of the polygon, firstRecord is the first record in attribute table
# resolution is the DGGS resolution required


def poly_to_DGGS_tool(myPoly, thisRecord,
                      resolution):  # one poly and the attribute record for it
    print()
    print()
    # get some info out of the first record
    print('call record', thisRecord)

    # find the bounding bow of the polygon
    bbox = myPoly.bbox
import pygeoj
from dggs import RHEALPixDGGS
rdggs = RHEALPixDGGS() # make an instance

'''
developed at Geoscience Australia by Joseph Bell June 2020
'''
testfile = pygeoj.load(filepath=r'D:\Grahaeme\EIT_geojson_example.geojson')

print('len', len(testfile)) # the number of features
print('bbox of entire file', testfile.bbox) # the bounding box region of the entire file

resolution = 10
# calc cell area
resArea = (rdggs.cell_area(resolution, plane=False))

#metadata
print('crs', testfile.crs) # the coordinate reference system
print('attributes', testfile.all_attributes) # retrieves the combined set of all feature attributes
print('common attributes', testfile.common_attributes) # retrieves only those field attributes that are common to all features
print()

# make an output file of DGGS centroid points with the at atttibute properties
newfile = pygeoj.new()  # default projection is WGS84

#work through the features (polygons) one by one and ask for DGGS cells
for feature in testfile:
    print('feature attributes ', feature.properties)  # the feature attributes - want to keep for output

    coords = feature.geometry.coordinates  # xy
    print('geom', coords)
Example #6
0
import pygeoj
from dggs import RHEALPixDGGS
rdggs = RHEALPixDGGS() # make an instance
import math

'''
developed by Joseph Bell at Geoscience Australia June 2020
'''
def densify_my_line(line_to_densify, resolution):
    '''
    densify a line based on the resolution of the cells
    designed to return a continuous string of ajoining DGGS cells along a line feature
    '''

    resArea = (rdggs.cell_area(resolution, plane=False))  # ask engine for area of cell
    # math to define a suitable distance between vertices - ensures good representation of the line - a continuous run of cells to define the line
    min_dist = math.sqrt(float(resArea))/300000  # width of cell changes with sqrt of the area - 300000 is a constant that can be changed but will change output

    try:
        # first try multi-line construct (on failure try single line)
        for line_points in line_to_densify:
            edgeData = []  # we are going to make a list of edges based on pairs of vertices
            previous = (0, 0)  # placeholder for previous point
            for vertex in line_points:
                #print(vertex)
                if previous != (0, 0):  # not the beginning
                    newEdgeMulti = (previous, vertex)
                    #print('new edge', newEdge)
                    edgeData.append(newEdgeMulti)
                    previous = vertex  # remember for the next iteration
                else:
reads a csv of DGGS corners generated by add_DGGS.py
and converts it into a shapefile centroids useing the DGGS cell reference
NB: Not sure of the accuracy of shapefile - seems to be working in total confirmity with other datasets - worked into arcGiS

Joseph Bell Geoscience Australia

'''

import shapefile
import csv
#import dggs
from dggs import RHEALPixDGGS
from dggs import Cell
import ellipsoids
E = ellipsoids.WGS84_ELLIPSOID_DEG
rdggs = RHEALPixDGGS(ellipsoid=E, north_square=1, south_square=2, N_side=3)

from geopy import distance

# read the file
# file with the source data

f = r"\\xxxxx\Geonames_withDGGS.csv"

#set up shapefile for output
w = shapefile.Writer(shapefile.POINT)
w.field('ID', 'C', '20')
w.field('Name', 'C', '70')
w.field('DGGSrHEALPix', 'C', '20')
w.field('LongiWGS84', 'C',
        '20')  #using 'C' = character = ensures the correct number