GIS open source library GEOS library learning tutorial (2): geometry in geos (Geometry)

foreword

  In the previous section, we learned the introduction of the GEOS library, environment compilation and sample codes. In this section, we will learn about various geometric graphics classes in geos, most of which are derived from the Geometry class. Geometry is the basic operation object in geos, so the Geometry class is the most important class.
  There are three main elements in geometric figures: point, line, and surface. Horizontal and vertical coordinates constitute a point, multiple points constitute a line, a circle constitutes a plane, and a mixture of points, lines and planes constitutes a geometric set. The corresponding classes in the GEOS library are:

  • Coordinates: Coordinate
  • 点:Point、MultiPoint
  • Line: LineString, MultiLineString (multiple lines), LinearRing (ring line)
  • 面:Polygon、MultiPolygon
  • Collection: GeometryCollection

  In geos, the smallest constituent unit is the coordinate, which is created by the Coordinate class, such as: Coordinate(2,3), which represents a point, the abscissa is 2, and the ordinate is 3. The creation of all geometric figures is It is done by class GeometryFactory. Therefore, before creating geometry, you can create a global GeometryFactory object;

GeometryFactory g_factory; //全局对象,所有的图形都由此对象创建

1. Coordinate-Coordinate

  The Coordinate class is a lightweight class for storing coordinates. It is different from Point, which is a subclass of Geometry.
Unlike Point type objects, which contain additional
reference system information such as envelopes, precision models, and spaces, a Coordinate only contains ordinate values ​​and access methods. A Coordinate object is a 2D point with an additional z-ordinate.
  From the description of the class, we can see that Coordinate is a special class that is not derived from the Geometry class.

Coordinate(double xNew=0.0, double yNew=0.0, double zNew=DoubleNotANumber);

2、点-Point / MultiPoint

  The point class is special. Compared with the Coordinate class, the Coordinate class is more like the point object we understand. The point class cannot be initialized by x, y coordinate values, it can only be created by the Coordinate class.

Point* createGeosPoint(double x, double y)
{
    
    
	Coordinate coordt(x, y);
	Point* pt = g_factory.createPoint(coordt);
	return pt;
}

MultiPoint* createGeosMultiPoint()
{
    
    
	double x, double y;
	Coordinate coordt1(x, y);
	Coordinate coordt2(x+1, y+1);

	std::vector<Coordinate> vecCoords;
	vecCoords.push_back(coordt1);
	vecCoords.push_back(coordt2);

	MultiPoint* mPt = g_factory.createMultiPoint(vecCoords);
	return mPt;
}

3、线-LineString / MultiLineString / LinearRing

  The main differences between the MulitLineString type and the LineString type in GeoJSON are as follows:

  • 1) A MultiLineString element can contain one or more disconnected line segments. These line segments are regarded as the same element and share the same attribute information. An element of LineString type can only contain one line segment, and the attribute information is exclusive to this line segment;

  • 2) When there are multiple adjacent line segments with the same attribute information, use the MultiLineString type, as long as one element (a record in the database) can be represented, but use the LineString type, but need to create multiple elements. Therefore, it is not difficult to find that the MultiLineString type saves more storage space;

  • 3) In the GeoJSON file, the coordinates attribute of the LineString class element is represented by a two-dimensional array, while the MultiLineString class element is represented by a three-dimensional array;

  • 4) The commonly used shp format vector file midline elements are represented by Polyline type, and LineString and MultiLineString are not strictly distinguished.

{
    "type": "Feature",
    "geometry": {
        "type": "MultiLineString",
        "coordinates": [
            [
                [119.283461766823521,35.113845473433457],
                [119.285033114198498,35.11405167501087]
            ],
            [
                [119.186893667167482,34.88690637041627],
                [119.186947247282234,34.890273599368562]
            ]
        ]
    }
}
{ 
    "type": "Feature",
    "geometry": {
        "type": "LineString",
        "coordinates": [
            [119.207185494071,34.9298513918505],
            [119.207555586034,34.9294932576001]
        ]
    }
}
LineString* createGeosLine(double x,double y, double offset)
{
    
    
    CoordinateArraySequence *cas=new CoordinateArraySequence(); //构建点序列
    cas->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 = g_factory.createLineString(cas);
    return ls;
}

//创建一条环线,与线的区别就是环线是闭合的。即第一个点和最后一点重合
LinearRing* createGeosRing(double x,double y,double offset)
{
    
    
    CoordinateArraySequence *cas=new CoordinateArraySequence(); //构建点序列
    cas->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)); //与第一个点相等
    LinearRing *lr = g_factory.createLinearRing(cas);
    return lr;
}

4、面-Polygon / MultiPolygon

//创建一个多边形,如果多边形内部没有孔洞实际上与环线是一样的
Polygon* createGeosPolygon(double x,double y,double offset)
{
    
    
    LinearRing *lr = createGeosRing(x,y,offset);
    Polygon *poly = g_factory.createPolygon(lr,NULL); //如果多边形中间没有孔洞,第二个参数设为NULL
    return poly;
}

Guess you like

Origin blog.csdn.net/m0_37251750/article/details/129773252