spatial-join
Perform spatial joins between two tables using specified geometry columns and join types such as intersects, within, contains, or touches. Ideal for analyzing spatial relationships in PostGIS databases via the MCP server.
Instructions
İki tablo arasında mekansal join işlemi yap
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| geometry_column1 | No | Tablo1 geometri kolonu (varsayılan: geom) | |
| geometry_column2 | No | Tablo2 geometri kolonu (varsayılan: geom) | |
| join_type | Yes | Join tipi | |
| limit | No | Sonuç limiti (varsayılan: 100) | |
| table1 | Yes | Birinci tablo adı | |
| table2 | Yes | İkinci tablo adı |
Implementation Reference
- server.ts:1670-1725 (handler)Handler function for the 'spatial-join' tool. Parses input using SpatialJoinSchema, sanitizes table and column names, selects appropriate PostGIS spatial predicate based on join_type, executes a JOIN query between two tables, and returns results with GeoJSON geometries.case "spatial-join": { const { table1, table2, join_type, geometry_column1, geometry_column2, limit, } = SpatialJoinSchema.parse(args); const sanitizedTable1 = table1.replace(/[^a-zA-Z0-9_]/g, ""); const sanitizedTable2 = table2.replace(/[^a-zA-Z0-9_]/g, ""); const sanitizedGeomCol1 = geometry_column1 || "geom"; const sanitizedGeomCol2 = geometry_column2 || "geom"; const spatialPredicate = { intersects: "ST_Intersects", within: "ST_Within", contains: "ST_Contains", touches: "ST_Touches", }[join_type]; const result = await client.query( ` SELECT t1.*, t2.*, ST_AsGeoJSON(t1.${sanitizedGeomCol1}) as t1_geometry, ST_AsGeoJSON(t2.${sanitizedGeomCol2}) as t2_geometry FROM ${sanitizedTable1} t1 JOIN ${sanitizedTable2} t2 ON ${spatialPredicate}(t1.${sanitizedGeomCol1}, t2.${sanitizedGeomCol2}) LIMIT $1; `, [limit] ); return { content: [ { type: "text", text: JSON.stringify( { table1: sanitizedTable1, table2: sanitizedTable2, join_type: join_type, spatial_predicate: spatialPredicate, results_count: result.rows.length, results: result.rows, }, null, 2 ), }, ], }; }
- server.ts:106-113 (schema)Zod schema defining the input parameters for the spatial-join tool, including two tables, join type, optional geometry columns, and result limit.const SpatialJoinSchema = z.object({ table1: z.string(), table2: z.string(), join_type: z.enum(["intersects", "within", "contains", "touches"]), geometry_column1: z.string().optional().default("geom"), geometry_column2: z.string().optional().default("geom"), limit: z.number().optional().default(100), });
- server.ts:873-900 (registration)Registration of the 'spatial-join' tool in the ListToolsRequestSchema handler, providing name, description, and input schema matching SpatialJoinSchema.name: "spatial-join", description: "İki tablo arasında mekansal join işlemi yap", inputSchema: { type: "object", properties: { table1: { type: "string", description: "Birinci tablo adı" }, table2: { type: "string", description: "İkinci tablo adı" }, join_type: { type: "string", enum: ["intersects", "within", "contains", "touches"], description: "Join tipi", }, geometry_column1: { type: "string", description: "Tablo1 geometri kolonu (varsayılan: geom)", }, geometry_column2: { type: "string", description: "Tablo2 geometri kolonu (varsayılan: geom)", }, limit: { type: "number", description: "Sonuç limiti (varsayılan: 100)", }, }, required: ["table1", "table2", "join_type"], }, },