利用JTS構(gòu)建R樹索引

空間索引(Spatial Indexing)

回憶下數(shù)據(jù)庫最基本的操作:增刪改查以及稍復(fù)雜些的比如連接操作程奠,基本都需要先鎖定數(shù)據(jù)位置,再執(zhí)行操作祭钉。而定位這個步驟瞄沙,如果沒有index,基本都是O(n)的時間復(fù)雜度慌核,這是一個非尘嗑常“耗時”的操作】遄浚“找”這個操作就需要定位垫桂。注意這里的定位不再是指在存儲器上的位置,而是在空間中的位置粟按,這里的空間诬滩,是由數(shù)據(jù)的維度張成的空間〖卣空間數(shù)據(jù)碱呼,也即是這些擁有多維度的數(shù)據(jù)蒙挑。這是空間數(shù)據(jù)的一個比較延展性的說法宗侦。但通常,空間數(shù)據(jù)都focus on 幾何類型數(shù)據(jù)忆蚀,比如點矾利,線姑裂,面,球等男旗,當(dāng)然這些點舶斧、線都是可以任意維度的。對于空間數(shù)據(jù)的搜索察皇,我們需要空間索引spatial index來提升搜素效率(速度)茴厉。目前主流數(shù)據(jù)(SQL server,MySQL,PostgreSQL,etc)都已加入了對spatial data的支持,這其中最主要的就是數(shù)據(jù)類型和索引的支持什荣。

R-tree

MBR

為了統(tǒng)一描述空間類型矾缓,比如點,線稻爬,面等嗜闻,Guttman首先提出了MBR的概念,即Minimum Bounding Box桅锄。MBR的含義是用一個最小的矩形(通常默認(rèn)矩形的邊平行于坐標(biāo)軸)琉雳,來框住這個幾何體。


MBR

R-tree

R-tree主要吸納了B+tree的思想友瘤,對數(shù)據(jù)進(jìn)行分割翠肘。


R-tree

JTS生成R樹索引

JTS提供了如下的空間數(shù)據(jù)類型,還提供了讀取各種空間描述文件(WTK等)辫秧,線簡化锯茄,空間操作(求交,計算距離茶没,計算外包矩形等)肌幽,建立空間索引等多種算法。
參考這篇文章:https://blog.csdn.net/wk1134314305/article/details/76408181
引入依賴包:

// Gradle
compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
// Maven
<dependency>
<groupId>com.vividsolutions</groupId>
    <artifactId>jts</artifactId>
    <version>1.13</version>
</dependency>

Geometry類:所有的空間數(shù)據(jù)類型抓半,點喂急,線,面笛求,多點廊移,環(huán),多邊形等等都是繼承自Geometry類的探入。
Envelope類:該類就是描述一個空間幾何對象的外包矩形狡孔,由max_x,max_y,min_x,min_y組成。

JTS常用的方法蜂嗽,以及R樹索引基本使用:

package com.geotools.geotoolsdemo;

import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: geotoolsdemo
 * @description: JTS導(dǎo)入使用
 * @author: zhudan
 * @create: 2020/7/3 11:39
 */
@Component
public class GeometryDemo {
    //使用JTS的GeometryFactory來創(chuàng)建Geometry對象
    @Autowired
    private GeometryFactory geometryFactory = new GeometryFactory();

    /**
     * @Description: 創(chuàng)建一個點
     * @return: com.vividsolutions.jts.geom.Point
     */
    public Point createPoint() {
        Coordinate coord = new Coordinate(118.798128, 31.968592);
        Point point = geometryFactory.createPoint(coord);
        return point;
    }

    /**
     * @Description: 從WKT創(chuàng)建一個點苗膝,WKT字符串創(chuàng)建 ,WKT字符串是SQL標(biāo)準(zhǔn)定義的一個實現(xiàn)
     * @return: com.vividsolutions.jts.geom.Point
     */
    public Point createPointByWKT() throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
        Point point = (Point) reader.read("POINT (118.798128 31.968592)");
        return point;
    }

    /**
     * @Description: 從WKT創(chuàng)建多個點
     * @return: com.vividsolutions.jts.geom.MultiPoint
     */
    @Autowired
    public MultiPoint createMulPointByWKT(String wellKnownText) throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
//        MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(119.013388 31.715519, 119.32488 31.435678)");
        MultiPoint mpoint = (MultiPoint) reader.read(wellKnownText);
        return mpoint;
    }

    /**
     * @Description: create a line
     * @return: com.vividsolutions.jts.geom.LineString
     */
    public LineString createLine() {
        Coordinate[] coords = new Coordinate[]{new Coordinate(119.013388, 31.715519), new Coordinate(119.32488, 31.435678)};
        LineString line = geometryFactory.createLineString(coords);
        return line;
    }

    /**
     * @Description: create a line by WKT
     * @return: com.vividsolutions.jts.geom.LineString
     */
    public LineString createLineByWKT() throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
        LineString line = (LineString) reader.read("LINESTRING(119.013388 31.715519, 119.32488 31.435678)");
        return line;
    }

    /**
     * @Description: create multiLine
     * @return: com.vividsolutions.jts.geom.MultiLineString
     */
    public MultiLineString createMLine() {
        Coordinate[] coords1 = new Coordinate[]{new Coordinate(119.013388, 31.715519), new Coordinate(119.32488, 31.435678)};
        LineString line1 = geometryFactory.createLineString(coords1);
        Coordinate[] coords2 = new Coordinate[]{new Coordinate(118.797499, 32.087104), new Coordinate(118.798128, 31.968592)};
        LineString line2 = geometryFactory.createLineString(coords2);
        LineString[] lineStrings = new LineString[2];
        lineStrings[0] = line1;
        lineStrings[1] = line2;
        MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);
        return ms;
    }

    /**
     * @Description: create multiLine by WKT
     * @return: com.vividsolutions.jts.geom.MultiLineString
     */
    public MultiLineString createMLineByWKT() throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
        MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((119.013388 31.715519, 119.32488 31.435678),(118.797499 32.087104,118.798128 31.968592))");
        return line;
    }

    /**
     * @Description: create a polygon(多邊形)
     * @return:
     */
    public Polygon createPolygon() {
        Coordinate[] coords = new Coordinate[]{new Coordinate(20, 10), new Coordinate(30, 0), new Coordinate(40, 10),
                new Coordinate(30, 20), new Coordinate(20, 10)};
        Polygon polygon = geometryFactory.createPolygon(coords);
        return polygon;
    }

    /**
     * @Description: create a polygon(多邊形) by WKT
     * @return:
     */
    public Polygon createPolygonByWKT() throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
        Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
        return polygon;
    }

    /**
     * @Description: create multi polygon(多邊形) by WKT
     * @return:
     */
    public MultiPolygon createMulPolygonByWKT() throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
        MultiPolygon mpolygon = (MultiPolygon) reader.read("MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40 10),(30 10, 30 0, 40 10, 30 20, 30 10)))");
        return mpolygon;
    }

    /**
     * @Description: create GeometryCollection  contain point or multiPoint or line or multiLine or polygon or multiPolygon
     * @return: com.vividsolutions.jts.geom.GeometryCollection
     */
    public GeometryCollection createGeoCollect() throws ParseException {
        LineString line = createLine();
        Polygon poly = createPolygonByWKT();
        Geometry g1 = geometryFactory.createGeometry(line);
        Geometry g2 = geometryFactory.createGeometry(poly);
        Geometry[] garray = new Geometry[]{g1, g2};
        GeometryCollection gc = geometryFactory.createGeometryCollection(garray);
        return gc;
    }

    /**
     * create a Circle  創(chuàng)建一個圓植旧,圓心(x,y) 半徑RADIUS
     *
     * @param x
     * @param y
     * @param RADIUS
     * @return
     */
    public Polygon createCircle(double x, double y, final double RADIUS) {
        final int SIDES = 32;//圓上面的點個數(shù)
        Coordinate coords[] = new Coordinate[SIDES + 1];
        for (int i = 0; i < SIDES; i++) {
            double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
            double dx = Math.cos(angle) * RADIUS;
            double dy = Math.sin(angle) * RADIUS;
            coords[i] = new Coordinate((double) x + dx, (double) y + dy);
        }
        coords[SIDES] = coords[0];
        LinearRing ring = geometryFactory.createLinearRing(coords);
        Polygon polygon = geometryFactory.createPolygon(ring, null);
        return polygon;
    }

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        GeometryDemo gt = new GeometryDemo();
        Polygon p1 = gt.createCircle(0, 1, 2);
        //圓上所有的坐標(biāo)(32個)
//        Coordinate coords[] = p1.getCoordinates();
//        for (Coordinate coord : coords) {
//            System.out.println(coord.x + "," + coord.y);
//        }

        Polygon p2 = gt.createCircle(1, 0, 2);
        STRtree stRtree = new STRtree();
        stRtree.insert(p1.getEnvelopeInternal(), p1);
        stRtree.insert(p2.getEnvelopeInternal(), p2);

        List results = stRtree.query(new Envelope(0, 0, 0, 0));
        for (int i = 0; i < results.size(); i++) {
            System.out.println(results.get(i));
        }
    }
}

參考:
https://zhuanlan.zhihu.com/p/38597148
https://blog.csdn.net/wk1134314305/article/details/76408181

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辱揭,一起剝皮案震驚了整個濱河市离唐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌问窃,老刑警劉巖亥鬓,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異域庇,居然都是意外死亡嵌戈,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門听皿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晨仑,“玉大人呻畸,你說我怎么就攤上這事菇夸“褪粒” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵啊送,是天一觀的道長偿短。 經(jīng)常有香客問我,道長馋没,這世上最難降的妖魔是什么昔逗? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮篷朵,結(jié)果婚禮上勾怒,老公的妹妹穿的比我還像新娘。我一直安慰自己声旺,他們只是感情好笔链,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著腮猖,像睡著了一般鉴扫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上澈缺,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天坪创,我揣著相機(jī)與錄音,去河邊找鬼姐赡。 笑死莱预,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的项滑。 我是一名探鬼主播依沮,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了悉抵?” 一聲冷哼從身側(cè)響起肩狂,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤摘完,失蹤者是張志新(化名)和其女友劉穎姥饰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孝治,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡列粪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了谈飒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片岂座。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖杭措,靈堂內(nèi)的尸體忽然破棺而出费什,到底是詐尸還是另有隱情,我是刑警寧澤手素,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布鸳址,位于F島的核電站,受9級特大地震影響泉懦,放射性物質(zhì)發(fā)生泄漏稿黍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一崩哩、第九天 我趴在偏房一處隱蔽的房頂上張望巡球。 院中可真熱鬧,春花似錦邓嘹、人聲如沸酣栈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钉嘹。三九已至,卻和暖如春鲸阻,著一層夾襖步出監(jiān)牢的瞬間跋涣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工鸟悴, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留陈辱,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓细诸,卻偏偏與公主長得像沛贪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354