The second study of GEOS Library: creation of simple geometry

Source: Internet
Author: User

Geometry (Geometry) is the basic operand within geos, so the Geometry class is the most important class

There are three main elements in the geometry: point, line, polygon. The horizontal ordinate constituent point, the multiple points constitute the line, the Circle line constitutes the surface, the point lines surface mix constitutes the geometric set. Several of the corresponding classes are

Coordinates: Coordinate

Points: Point, MultiPoint

Lines: LineString, multilinestring (multiple lines), linearring (Loop)

Noodles: Polygon, Multipolygon

Collection: GeometryCollection

In GeoS, the smallest constituent units are coordinates, created by the coordinate class, such as: coordinate (2,3), which represents a point, the horizontal axis is 2, and the ordinate is 3.

The creation of all geometries is done by the class geometryfactory. Therefore, before creating the geometry, you can create a global geometryfactory object;

// global objects, all of which are created by this object

1, the creation of the point

point* creategeospoint (double x,double  y) {    coordinate pt (x, y);      Point * p=factory->createpoint (PT);     return p;}

2, the creation of non-closed lines

linestring* Creategeosline (DoubleXDoubleYDoubleoffset) {coordinatearraysequence*cas=NewCoordinatearraysequence ();//Building Point SequencesCas->Add (coordinate (x, y)); CAS->add (Coordinate (x,y+offset)); CAS->add (Coordinate (x+offset,y+offset)); CAS->add (Coordinate (x+offset,y+2*offset)); CAS->add (Coordinate (x+2*offset,y+2*offset)); LineString*ls=factory->createlinestring (CAS); returnls;}

3, the creation of closed lines

//Create a loop, the difference between the line is that the loop is closed. That is, the first point coincides with the last one.linearring* creategeosring (DoubleXDoubleYDoubleoffset) {coordinatearraysequence*cas=NewCoordinatearraysequence ();//Building Point SequencesCas->Add (coordinate (x, y)); CAS->add (Coordinate (x,y+offset)); CAS->add (Coordinate (x+offset,y+offset)); CAS->add (Coordinate (x+offset,y+2*offset)); CAS->add (Coordinate (x+2*offset,y+2*offset)); CAS->add (Coordinate (x+2*offset,y)); CAS->add (Coordinate (x, y));//equal to the first pointLinearring *lr=factory->createlinearring (CAS); returnLR;}

In addition to using the Add method to build the point sequence, you can also use another method SetAt

linearring* creategeosring (DoubleXDoubleYDoubleoffset)     {Coordinatearraysequencefactory CSF; Coordinatesequence* cs = csf.create (7,2); CS->setat (Coordinate (x, y),0); CS->setat (Coordinate (x,y+offset),1); CS->setat (Coordinate (x+offset,y+offset),2); CS->setat (Coordinate (x+offset,y+2*offset),3); CS->setat (Coordinate (x+2*offset,y+2*offset),4); CS->setat (Coordinate (x+2*offset,y),5); CS->setat (Coordinate (x, y),6);//equal to the first pointLinearring *lr=factory->createlinearring (CS); returnLR;}

4, the creation of polygons

// Create a polygon, if there is no hole inside the polygon is actually the same as the loop polygon* Creategeospolygon (double x,double y,double  offset) {    *lr=  Creategeosring (X,y,offset);     // if there is no hole in the middle of the polygon, the second parameter is set to null    return Poly;}

Test:

#include"Geos.h"intMain () {LineString*ls=creategeosring (Ten,Ten,5); cout<<"Line points:"<<ls->getnumpoints () <<"Line Length:"<<ls->getlength () <<Endl; Polygon*poly=creategeospolygon (Ten,Ten,5); cout<<"Polygon Area:"<<poly->getarea () <<Endl; System ("Pause"); return 1;}

The second study of GEOS Library: creation of simple geometry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.