Вопрос

I was wondering if anyone could help. I am always asked by my colleagues to create a postcode lookup table for them and this takes me considerable time using a desktop GIS. I was wondering if it is possible to create a table using SQL Server that would create the Lookup for me?

The Lookup table is based on finding the relevant boundaries for a postcode so a sample table would look like this:

Table A
POSTCODE  SCHOOL     WARD        POLLING DISTRICT
BH15 2RU  ST PETERS  ELMSDALE    PD

All of the information for School, Ward and Polling district are coming from different tables. Each of these tables has a geometry column (as has the postcode table).

I can run a select statement to do a simple join (say postcode to school) and create that table, but I would like to run all of the separate spatial queries in one large query to create the singular table. I have about 20 or so different tables with boundaries that are required for the large lookup table.

I hope that makes sense!

Any help would be greatly appreciated.

Это было полезно?

Решение

First you'll need to create your table, a one-off process:

CREATE TABLE [Example]
(
    Postcode VARCHAR(8) NOT NULL,
    School VARCHAR(50),
    Ward VARCHAR(50)
    -- Repeat for each row (setting appropriate length / type etc)
);

Then you can populate them like this (suggest making a stored procedure, index on Example.Postcode and ensure appropriate Spatial Index on all geometry columns):

MERGE [Example] AS TARGET
USING
(
SELECT
    P.[Name] AS PostcodeName,
    SCH.[Name] AS SchoolName,
    WRD.[Name] AS WardName
    -- Other variables as required
FROM
    [Postcodes] P
    JOIN [Schools] SCH ON SCH.[Geometry].STIntersects(P.[Geometry]) = 1
    JOIN [Wards] WRD ON WRD.[Geometry].STIntersects(P.[Geometry]) = 1
    -- OTHER JOINS AS REQUIRED
)
AS SOURCE
ON TARGET.[Postcode] = SOURCE.[PostcodeName]
WHEN MATCHED BY TARGET
THEN
    UPDATE SET
    [School] = SOURCE.[SchoolName],
    [Ward] = SOURCE.[WardName]
    -- Repeat for other variables
WHEN NOT MATCHED BY TARGET
THEN
    INSERT
    ([Postcode], [School], [Ward]) -- etc.
    VALUES
    (SOURCE.[PostcodeName], SOURCE.[SchoolName], SOURCE.[WardName]) -- etc.
WHEN NOT MATCHED BY SOURCE -- Only include this if you want to remove any records where the Postcode has have been deleted since the last "run"
THEN
    DELETE;

I haven't tested obviously, but it should be 99%+ of the way there.

MERGE if you haven't used it provides a wonderful way to handle insert / update / delete in a single statement. I am also making the assumption that your geography data is well formed, they all share the same (or no) SRID, they all specify coordinates in the same datum / projection), and that there are no overlaps in each table to deal with. Also, please note that even with a lot of ooompfh, this make take some time to run if you have national coverage (1.7m postcodes, 225k OA's, 9K wards etc.). I suggest you add a WHERE Postcode.[Name] LIKE 'BH%' or similar to test first.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top