geometryfactory.cpp
来自「一个很好的vc底层代码」· C++ 代码 · 共 848 行 · 第 1/2 页
CPP
848 行
LinearRing*GeometryFactory::createLinearRing(CoordinateSequence* newCoords) const{ return new LinearRing(newCoords,this);}/* * Creates a LinearRing using a copy of the given CoordinateSequence. * An empty CoordinateSequence will create an empty LinearRing. * The points must form a closed and simple * linestring. Consecutive points must not be equal. * @param fromCoords a CoordinateSequence possibly empty. */LinearRing*GeometryFactory::createLinearRing(const CoordinateSequence& fromCoords) const{ CoordinateSequence *newCoords = fromCoords.clone(); LinearRing *g = NULL; // construction failure will delete newCoords g = new LinearRing(newCoords, this); return g;}/** * Constructs a <code>MultiPoint</code>. * * @param newPoints * the <code>Point</code>s for this <code>MultiPoint</code>, * or <code>null</code> or an empty array to create the empty * geometry. * Elements may be empty <code>Point</code>s, * but not <code>null</code>s. * * Constructed object will take ownership of * the vector and its elements. */MultiPoint*GeometryFactory::createMultiPoint(vector<Geometry *> *newPoints) const{ return new MultiPoint(newPoints,this);}/** * Constructs a <code>MultiPoint</code>. * * @param fromPoints * the <code>Point</code>s for this <code>MultiPoint</code>, * or an empty array to create the empty geometry. * Elements may be empty <code>Point</code>s, * but not <code>null</code>s. * * Constructed object will copy * the vector and its elements. */MultiPoint*GeometryFactory::createMultiPoint(const vector<Geometry *> &fromPoints) const{ vector<Geometry *>*newGeoms = new vector<Geometry *>(fromPoints.size()); for (unsigned int i=0; i<fromPoints.size(); i++) { (*newGeoms)[i] = fromPoints[i]->clone(); } MultiPoint *g = NULL; try { g = new MultiPoint(newGeoms,this); }
catch (...) { for (unsigned int i=0; i<newGeoms->size(); i++) { delete (*newGeoms)[i]; } delete newGeoms; throw; } return g;}/** * Creates an EMPTY MultiPoint */MultiPoint*GeometryFactory::createMultiPoint() const{ return new MultiPoint(NULL, this);}/** * Creates a MultiPoint using the given CoordinateSequence. * @param fromCoords a CoordinateSequence used for Points construction. */MultiPoint*GeometryFactory::createMultiPoint(const CoordinateSequence &fromCoords) const{ vector<Geometry *> *pts=new vector<Geometry *>(); for (int i=0; i<fromCoords.getSize(); i++) { Point *pt=createPoint(fromCoords.getAt(i)); pts->push_back(pt); } MultiPoint *mp = NULL; try { mp = createMultiPoint(pts); }
catch (...) { for (unsigned int i=0; i<pts->size(); i++)
delete (*pts)[i]; delete pts; throw; } return mp;}/** * Constructs an EMPTY Polygon */Polygon*GeometryFactory::createPolygon() const{ return new Polygon(NULL, NULL, this);}/** * Constructs a <code>Polygon</code> with the given exterior boundary and * interior boundaries. * * @param shell * the outer boundary of the new <code>Polygon</code>, or * <code>null</code> or an empty <code>LinearRing</code> if * the empty geometry is to be created. * @param holes * the inner boundaries of the new <code>Polygon</code>, or * <code>null</code> or empty <code>LinearRing</code> s if * the empty geometry is to be created. */Polygon*GeometryFactory::createPolygon(LinearRing *shell, vector<Geometry *> *holes) const{ return new Polygon(shell, holes, this);}/** * Constructs a <code>Polygon</code> with the given exterior boundary and * interior boundaries. * * @param shell * the outer boundary of the new <code>Polygon</code> * @param holes * the inner boundaries of the new <code>Polygon</code> * Note that these must be LinearRings */Polygon*GeometryFactory::createPolygon(const LinearRing &shell, const vector<Geometry *> &holes) const{ LinearRing *newRing = (LinearRing *)shell.clone(); vector<Geometry *>*newHoles = new vector<Geometry *>(holes.size()); for (unsigned int i=0; i<holes.size(); i++) { (*newHoles)[i] = holes[i]->clone(); } Polygon *g = NULL; try { g = new Polygon(newRing, newHoles, this); }
catch (...) { delete newRing; for (unsigned int i=0; i<holes.size(); i++) delete (*newHoles)[i]; delete newHoles; throw; } return g;}/** * Constructs an EMPTY LineString */LineString *GeometryFactory::createLineString() const{ return new LineString(NULL, this);}/** * Constructs a <code>LineString</code> taking ownership of the * given CoordinateSequence. * * @param newCoords the list of coordinates making up the linestring, * or <code>null</code> to create the empty geometry. * Consecutive points may not be equal. Created object will * take ownership of CoordinateSequence. * */ LineString*GeometryFactory::createLineString(CoordinateSequence *newCoords) const{ return new LineString(newCoords, this);}/** * Constructs a <code>LineString</code> copying the * given CoordinateSequence. * * @param fromCoords the list of coordinates making up the linestring. * Consecutive points may not be equal. */ LineString*GeometryFactory::createLineString(const CoordinateSequence &fromCoords) const{ CoordinateSequence *newCoords = fromCoords.clone(); LineString *g = NULL; // construction failure will delete newCoords g = new LineString(newCoords, this); return g;}/*** Build an appropriate <code>Geometry</code>, <code>MultiGeometry</code>, or* <code>GeometryCollection</code> to contain the <code>Geometry</code>s in* it.** For example:** - If <code>geomList</code> contains a single <code>Polygon</code>,* the <code>Polygon</code> is returned.* - If <code>geomList</code> contains several <code>Polygon</code>s, a* <code>MultiPolygon</code> is returned.* - If <code>geomList</code> contains some <code>Polygon</code>s and* some <code>LineString</code>s, a <code>GeometryCollection</code> is* returned.* - If <code>geomList</code> is empty, an empty* <code>GeometryCollection</code> is returned* .** Note that this method does not "flatten" Geometries in the input,* and hence if any MultiGeometries are contained in the input a* GeometryCollection containing them will be returned.** @param newGeoms the <code>Geometry</code>s to combine** @return* A <code>Geometry</code> of the "smallest", "most type-specific"* class that can contain the elements of <code>geomList</code>.** NOTE: the returned Geometry will take ownership of the* given vector AND its elements */Geometry*GeometryFactory::buildGeometry(vector<Geometry *> *newGeoms) const{ string geomClass("NULL"); bool isHeterogeneous=false; bool isCollection=newGeoms->size()>1; unsigned int i; for (i=0; i<newGeoms->size(); i++) { string partClass(typeid(*(*newGeoms)[i]).name()); if (geomClass=="NULL") { geomClass=partClass; }
else if (geomClass!=partClass) { isHeterogeneous = true; } } // for the empty geometry, return an empty GeometryCollection if (geomClass=="NULL") { // we do not need the vector anymore delete newGeoms; return createGeometryCollection(); } if (isHeterogeneous) { return createGeometryCollection(newGeoms); } // At this point we know the collection is not hetereogenous. // Determine the type of the result from the first Geometry in the // list. This should always return a geometry, since otherwise // an empty collection would have already been returned Geometry *geom0=(*newGeoms)[0]; if (isCollection) { if (typeid(*geom0)==typeid(Polygon)) { return createMultiPolygon(newGeoms); }
else if (typeid(*geom0)==typeid(LineString)) { return createMultiLineString(newGeoms); }
else if (typeid(*geom0)==typeid(LinearRing)) { return createMultiLineString(newGeoms); }
else if (typeid(*geom0)==typeid(Point)) { return createMultiPoint(newGeoms); }
else { return createGeometryCollection(newGeoms); } } // since this is not a collection we can delete vector delete newGeoms; return geom0;}/** * This function does the same thing of the omonimouse function * taking vector pointer instead of reference. * The difference is that this version will copy needed data * leaving ownership to the caller. */Geometry*GeometryFactory::buildGeometry(const vector<Geometry *> &fromGeoms) const{ string geomClass("NULL"); bool isHeterogeneous=false; bool isCollection=fromGeoms.size()>1; unsigned int i; for (i=0; i<fromGeoms.size(); i++) { string partClass(typeid(*fromGeoms[i]).name()); if (geomClass=="NULL") { geomClass=partClass; }
else if (geomClass!=partClass) { isHeterogeneous = true; } } // for the empty geometry, return an empty GeometryCollection if (geomClass=="NULL") { return createGeometryCollection(); } if (isHeterogeneous) { return createGeometryCollection(fromGeoms); } // At this point we know the collection is not hetereogenous. // Determine the type of the result from the first Geometry in the // list. This should always return a geometry, since otherwise // an empty collection would have already been returned Geometry *geom0=fromGeoms[0]; if (isCollection) { if (typeid(*geom0)==typeid(Polygon)) { return createMultiPolygon(fromGeoms); }
else if (typeid(*geom0)==typeid(LineString)) { return createMultiLineString(fromGeoms); }
else if (typeid(*geom0)==typeid(LinearRing)) { return createMultiLineString(fromGeoms); }
else if (typeid(*geom0)==typeid(Point)) { return createMultiPoint(fromGeoms); } Assert::shouldNeverReachHere("buildGeomtry encountered an unkwnon geometry type"); } return geom0->clone();}/** * @return a clone of g based on a CoordinateSequence created by this * GeometryFactory's CoordinateSequenceFactory */Geometry*GeometryFactory::createGeometry(const Geometry *g) const{ // could this be cached to make this more efficient? Or maybe it isn't enough overhead to bother return g->clone(); //GeometryEditor *editor=new GeometryEditor(this); //gfCoordinateOperation *coordOp = new gfCoordinateOperation(); //Geometry *ret=editor->edit(g, coordOp); //delete coordOp; //delete editor; //return ret;}/** * Destroy a Geometry, or release it. */voidGeometryFactory::destroyGeometry(Geometry *g) const{ delete g;}} // namespace geos/********************************************************************** * $Log: GeometryFactory.cpp,v $ * Revision 1.50.2.3 2005/06/22 00:46:34 strk * Fixed bugus handling of collections in ::buildGeometry * * Revision 1.50.2.2 2005/06/17 14:58:38 strk * Fixed segfault in LinearRing and LineString constructors * * Revision 1.50.2.1 2005/05/24 07:31:12 strk * Input checking and promoting in GeometryFactory::createMultiLineString() * * Revision 1.50 2004/07/27 16:35:46 strk * Geometry::getEnvelopeInternal() changed to return a const Envelope *. * This should reduce object copies as once computed the envelope of a * geometry remains the same. * * Revision 1.49 2004/07/19 13:19:30 strk * Documentation fixes * * Revision 1.48 2004/07/13 08:33:52 strk * Added missing virtual destructor to virtual classes. * Fixed implicit unsigned int -> int casts * * Revision 1.47 2004/07/08 19:34:49 strk * Mirrored JTS interface of CoordinateSequence, factory and * default implementations. * Added DefaultCoordinateSequenceFactory::instance() function. * * Revision 1.46 2004/07/05 19:40:48 strk * Added GeometryFactory::destroyGeometry(Geometry *) * **********************************************************************/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?