问题
I am attempting to be create a polygon table of hexagons, with a name for each hexagon (e.g. Hex1, Hex2, etc...) and join the polygon table to a table of records that have x and y coordinates if the coordinates fall within a polygon.
Creating the hex table with the code below, when ran in isolation, seems to be working (except for some of my numbers being off, creating some overlap)
DECLARE @Hex TABLE --Create table to old hexagons
(PointGeom geometry, Hex varchar(6))
INSERT INTO @Hex --Create hexagons, name, and put into table
VALUES
(geometry::STPolyFromText('Polygon((7517683.77026919 737591.11, 7517395.09513459 738091.11, 7516817.74486541 738091.11, 7516529.06973081 737591.11, 7516817.74486541 737091.11, 7517395.09513459 737091.11, 7517683.77026919 737591.11))', 0),'Hex1'),
(geometry::STPolyFromText('Polygon((7518549.79567297 737158.097298108, 7518261.12053838 737658.097298108, 7517683.77026919 737658.097298108, 7517395.09513459 737158.097298108, 7517683.77026919 736658.097298108, 7518261.12053838 736658.097298108, 7518549.79567297 737158.097298108))', 0),'Hex2');
To create points from my existing/source table (I cannot alter the source table), for which @Hex will be joined to, I have tried the following code
Drop Table #Points
create table #Points
(Primary_key bigint identity not null, geom geometry)
SET IDENTITY_INSERT #Points ON
INSERT INTO #Points (Primary_Key, Geom)
select g.primary_key, geometry::STGeomFromText('POINT('+convert(varchar(20),g.x_coordinate)+' '+convert(varchar(20),g.y_coordinate)+')',4326) as geom
from go_data g
SET IDENTITY_INSERT #Points OFF
Finally, to get my desired result the following code
select g.primary_key, Hex
from g --source table
left join #points p on g.primary_key = p.Primary_key
left join @hex h
on p.geom.STIntersects (h.hex) =1
The code included above has been frankensteined together from various forum posts and research that I have done so far (I am way outside my knowledge base). So my approach my be way off.
In the codes current iteration, it is 'running', but I gave it about 60 minutes and it had not finished. The final code will have about 2000 polygons (currently testing with 6) and be joined to a record set of 100,000+ records.
The desired result is:
Record1 Hex1 Record2 Hex2 Record3 Hex1 Record4 Hex1 Reocrd5 Hex2
来源:https://stackoverflow.com/questions/57318017/creating-a-table-with-points-from-x-and-y-coordinates-and-joining-to-a-table-of