เก็บไว้กันลืม

มาตรฐาน

โค๊ดสำหรับ intersec พวก gis

พอดีไปปรับแก้นิดนึงให้ใช้ภาษาไทยได้
credit–>http://gis.stackexchange.com/questions/119374/intersect-shapefiles-using-shapely

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from osgeo import ogr
os.environ['SHAPE_ENCODING'] = "utf-8"
ogr.UseExceptions()
ogr_ds = ogr.Open('./', True)  # Windows: r'C:\path\to\data'

SQL = """\
    SELECT ST_Intersection(A.geometry, B.geometry) AS geometry, A.*, B.*
    FROM tb A, code B
    WHERE ST_Intersects(A.geometry, B.geometry);
"""

ogr_ds.ExecuteSQL('PRAGMA encoding="UTF-8";', dialect='SQLITE')
#layer = ogr_ds.ExecuteSQL(SQL, dialect='SQLITE')
#ogr_ds.setCPLConfigOption("SHAPE_ENCODING", "UTF-8")
layer = ogr_ds.ExecuteSQL(SQL,dialect='SQLITE')
# copy result back to datasource as a new shapefile
layer2 = ogr_ds.CopyLayer(layer, 'newone')
# save, close
layer = layer2 = ogr_ds = None

อันนี้ก็อีก Code นึง สำหรับ django โดยเฉพาะ

from django.db import connections
cursor = connections['my_db_alias'].cursor()
cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
SQL='''
    SELECT basin.*, province_border.*
    FROM basin, province_border
    WHERE ST_Intersects(basin.the_geom, province_border.the_geom);
'''
cursor.fetchall()
cursor.fetchone()
Advertisements

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s