unsigned char* QgsDistanceArea::measureLine( unsigned char* feature, double* area, bool hasZptr )
{
  unsigned char *ptr = feature + 5;
  unsigned int nPoints = *(( int* )ptr );
  ptr = feature + 9;

  QList<QgsPoint> points;
  double x, y;

  QgsDebugMsg( "This feature WKB has " + QString::number( nPoints ) + " points" );
  // Extract the points from the WKB format into the vector
  for ( unsigned int i = 0; i < nPoints; ++i )
  {
    x = *(( double * ) ptr );
    ptr += sizeof( double );
    y = *(( double * ) ptr );
    ptr += sizeof( double );
    if ( hasZptr )
    {
      // totally ignore Z value
      ptr += sizeof( double );
    }

    points.append( QgsPoint( x, y ) );
  }

  *area = measureLine( points );
  return ptr;
}
const unsigned char* QgsDistanceArea::measureLine( const unsigned char* feature, double* area, bool hasZptr )
{
  QgsConstWkbPtr wkbPtr( feature + 1 + sizeof( int ) );
  int nPoints;
  wkbPtr >> nPoints;

  QList<QgsPoint> points;
  double x, y;

  QgsDebugMsg( "This feature WKB has " + QString::number( nPoints ) + " points" );
  // Extract the points from the WKB format into the vector
  for ( int i = 0; i < nPoints; ++i )
  {
    wkbPtr >> x >> y;
    if ( hasZptr )
    {
      // totally ignore Z value
      wkbPtr += sizeof( double );
    }

    points.append( QgsPoint( x, y ) );
  }

  *area = measureLine( points );
  return wkbPtr;
}
Exemple #3
0
int
reflow(struct textLine *ln, int tillend)
{
	int len, i;
	struct textLine *nextln;
	while (ln && ln->next && !ln->br) {
		nextln = ln->next;
		if ((ln->selected && !nextln->selected)
		    || (!ln->selected && nextln->selected)) {
			ln = nextln;
			continue;
		}
		if (!ln->len) {
			nextln->prev = ln->prev;
			if (nextln->next)
				nextln->next->prev = ln;
			keepSelect(ln);
			*ln = *nextln;	//curline 可能正指向 ln, 所以释放 nextln 而不是 ln
			free(nextln);
			ln->changed = 1;
			continue;
		}
		len = min(MAXLINELEN - ln->len, nextln->len);
		if (len) {
			memcpy(ln->text + ln->len, nextln->text, len);
			//从下一行抓取的字符数
			len =
			    measureLine(ln->text, ln->len + len, MAXLINELEN, 0,
					&i) - ln->len;
		}
		if (nextln->len && len <= 0)
			break;
		ln->len += len;
		nextln->len -= len;
		memmove(nextln->text, nextln->text + len, nextln->len);
		ln->changed = 1;
		nextln->changed = 1;
		if (!nextln->len
		    && measureWidth(ln->text, ln->len) < MAXLINELEN) {
			ln->next = nextln->next;
			if (nextln->next)
				nextln->next->prev = ln;
			if (nextln->br)
				ln->br = 1;
			keepSelect(nextln);
			free(nextln);
			if (ln->br)
				break;
			nextln = ln->next;
		}
		ln = nextln;
	}
	return 0;
}
double QgsDistanceArea::measureLine( const QgsCurveV2* curve ) const
{
  if ( !curve )
  {
    return 0.0;
  }

  QList<QgsPointV2> linePointsV2;
  QList<QgsPoint> linePoints;
  curve->points( linePointsV2 );
  QgsGeometry::convertPointList( linePointsV2, linePoints );
  return measureLine( linePoints );
}
double QgsDistanceArea::measureLine( const QList<QgsPoint> &points ) const
{
  if ( points.size() < 2 )
    return 0;

  double total = 0;
  QgsPoint p1, p2;

  try
  {
    if ( mEllipsoidalMode && ( mEllipsoid != GEO_NONE ) )
      p1 = mCoordTransform->transform( points[0] );
    else
      p1 = points[0];

    for ( QList<QgsPoint>::const_iterator i = points.begin(); i != points.end(); ++i )
    {
      if ( mEllipsoidalMode && ( mEllipsoid != GEO_NONE ) )
      {
        p2 = mCoordTransform->transform( *i );
        total += computeDistanceBearing( p1, p2 );
      }
      else
      {
        p2 = *i;
        total += measureLine( p1, p2 );
      }

      p1 = p2;
    }

    return total;
  }
  catch ( QgsCsException &cse )
  {
    Q_UNUSED( cse );
    QgsMessageLog::logMessage( QObject::tr( "Caught a coordinate system exception while trying to transform a point. Unable to calculate line length." ) );
    return 0.0;
  }

}
Exemple #6
0
static void
insertChar(struct edit *edit, const char ch)
{
	struct textLine *ln = edit->curline, *newln;
	int len, width;
	if (!edit->overwrite)
		memmove(ln->text + edit->col + 1, ln->text + edit->col,
			ln->len - edit->col);
	ln->text[edit->col] = ch;
	edit->col++;
	edit->colcur = measureWidth(ln->text, edit->col);
	if (!edit->overwrite || edit->col > ln->len)
		ln->len++;
	ln->changed = 1;
	len = measureLine(ln->text, ln->len, MAXLINELEN, 0, &width);
	if (len == ln->len && width < MAXLINELEN) {
		edit->position = edit->colcur;
		return;
	}
	newln = allocNextLine(ln);
	newln->br = ln->br;
	ln->br = 0;
	memcpy(newln->text, ln->text + len, ln->len - len);
	newln->len = ln->len - len;
	ln->len = len;
	edit->redrawall = 1;
	reflow(newln, 0);
	if (edit->col > len || edit->colcur >= MAXLINELEN) {
		edit->curline = ln->next;
		edit->col = edit->col - len;
		edit->colcur = measureWidth(edit->curline->text, edit->col);
		edit->line++;
		edit->linecur++;
	}
	edit->position = edit->colcur;
}
double QgsDistanceArea::measureLine( const QgsPoint &p1, const QgsPoint &p2 ) const
{
  QGis::UnitType units;
  return measureLine( p1, p2, units );
}
double QgsDistanceArea::measure( const QgsAbstractGeometryV2* geomV2, MeasureType type ) const
{
  if ( !geomV2 )
  {
    return 0.0;
  }

  int geomDimension = geomV2->dimension();
  if ( geomDimension <= 0 )
  {
    return 0.0;
  }

  MeasureType measureType = type;
  if ( measureType == Default )
  {
    measureType = ( geomDimension == 1 ? Length : Area );
  }

  if ( !mEllipsoidalMode || mEllipsoid == GEO_NONE )
  {
    //no transform required
    if ( measureType == Length )
    {
      return geomV2->length();
    }
    else
    {
      return geomV2->area();
    }
  }
  else
  {
    //multigeom is sum of measured parts
    const QgsGeometryCollectionV2* collection = dynamic_cast<const QgsGeometryCollectionV2*>( geomV2 );
    if ( collection )
    {
      double sum = 0;
      for ( int i = 0; i < collection->numGeometries(); ++i )
      {
        sum += measure( collection->geometryN( i ), measureType );
      }
      return sum;
    }

    if ( measureType == Length )
    {
      const QgsCurveV2* curve = dynamic_cast<const QgsCurveV2*>( geomV2 );
      if ( !curve )
      {
        return 0.0;
      }

      QgsLineStringV2* lineString = curve->curveToLine();
      double length = measureLine( lineString );
      delete lineString;
      return length;
    }
    else
    {
      const QgsSurfaceV2* surface = dynamic_cast<const QgsSurfaceV2*>( geomV2 );
      if ( !surface )
        return 0.0;

      QgsPolygonV2* polygon = surface->surfaceToPolygon();

      double area = 0;
      const QgsCurveV2* outerRing = polygon->exteriorRing();
      area += measurePolygon( outerRing );

      for ( int i = 0; i < polygon->numInteriorRings(); ++i )
      {
        const QgsCurveV2* innerRing = polygon->interiorRing( i );
        area -= measurePolygon( innerRing );
      }
      delete polygon;
      return area;
    }
  }
}
unsigned char* QgsDistanceArea::measurePolygon( unsigned char* feature, double* area, double* perimeter, bool hasZptr )
{
  // get number of rings in the polygon
  unsigned int numRings = *(( int* )( feature + 1 + sizeof( int ) ) );

  if ( numRings == 0 )
    return 0;

  // Set pointer to the first ring
  unsigned char* ptr = feature + 1 + 2 * sizeof( int );

  QList<QgsPoint> points;
  QgsPoint pnt;
  double x, y;
  if ( area )
    *area = 0;
  if ( perimeter )
    *perimeter = 0;

  try
  {
    for ( unsigned int idx = 0; idx < numRings; idx++ )
    {
      int nPoints = *(( int* )ptr );
      ptr += 4;

      // Extract the points from the WKB and store in a pair of
      // vectors.
      for ( int jdx = 0; jdx < nPoints; jdx++ )
      {
        x = *(( double * ) ptr );
        ptr += sizeof( double );
        y = *(( double * ) ptr );
        ptr += sizeof( double );
        if ( hasZptr )
        {
          // totally ignore Z value
          ptr += sizeof( double );
        }

        pnt = QgsPoint( x, y );

        if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )
        {
          pnt = mCoordTransform->transform( pnt );
        }
        points.append( pnt );
      }

      if ( points.size() > 2 )
      {
        if ( area )
        {
          double areaTmp = computePolygonArea( points );
          if ( idx == 0 )
          {
            // exterior ring
            *area += areaTmp;
          }
          else
          {
            *area -= areaTmp; // interior rings
          }
        }

        if ( perimeter )
        {
          *perimeter += measureLine( points );
        }
      }

      points.clear();

      if ( !area )
      {
        break;
      }
    }
  }
  catch ( QgsCsException &cse )
  {
    Q_UNUSED( cse );
    QgsMessageLog::logMessage( QObject::tr( "Caught a coordinate system exception while trying to transform a point. Unable to calculate polygon area or perimeter." ) );
  }

  return ptr;
}
double QgsDistanceArea::measure( QgsGeometry* geometry )
{
  if ( !geometry )
    return 0.0;

  unsigned char* wkb = geometry->asWkb();
  if ( !wkb )
    return 0.0;

  unsigned char* ptr;
  unsigned int wkbType;
  double res, resTotal = 0;
  int count, i;

  memcpy( &wkbType, ( wkb + 1 ), sizeof( wkbType ) );

  // measure distance or area based on what is the type of geometry
  bool hasZptr = false;

  switch ( wkbType )
  {
    case QGis::WKBLineString25D:
      hasZptr = true;
    case QGis::WKBLineString:
      measureLine( wkb, &res, hasZptr );
      QgsDebugMsg( "returning " + QString::number( res ) );
      return res;

    case QGis::WKBMultiLineString25D:
      hasZptr = true;
    case QGis::WKBMultiLineString:
      count = *(( int* )( wkb + 5 ) );
      ptr = wkb + 9;
      for ( i = 0; i < count; i++ )
      {
        ptr = measureLine( ptr, &res, hasZptr );
        resTotal += res;
      }
      QgsDebugMsg( "returning " + QString::number( resTotal ) );
      return resTotal;

    case QGis::WKBPolygon25D:
      hasZptr = true;
    case QGis::WKBPolygon:
      measurePolygon( wkb, &res, 0, hasZptr );
      QgsDebugMsg( "returning " + QString::number( res ) );
      return res;

    case QGis::WKBMultiPolygon25D:
      hasZptr = true;
    case QGis::WKBMultiPolygon:
      count = *(( int* )( wkb + 5 ) );
      ptr = wkb + 9;
      for ( i = 0; i < count; i++ )
      {
        ptr = measurePolygon( ptr, &res, 0, hasZptr );
        resTotal += res;
      }
      QgsDebugMsg( "returning " + QString::number( resTotal ) );
      return resTotal;

    default:
      QgsDebugMsg( QString( "measure: unexpected geometry type: %1" ).arg( wkbType ) );
      return 0;
  }
}
Exemple #11
0
int
processOutput(FILE ** OUTPUT, FILE * OUTPUT_FILE, fSeq_t * mySequences, int numberOfSequences,
			  int numberOfThreads, printFormat_t * myFormat)
{
	int *seqsToPrint = NULL;
	char *myLine1;
	char *myLine2;
	long lineSize;
	int i, j, k;
	int outputFormat = myFormat->outputFormat;
	int linesBefore = myFormat->linesBefore;
	int linesAfter = myFormat->linesAfter;
	int printLabels = myFormat->printLabels;

	// If we're using plain text output, then we need to record
	// which sequences we're going to print out.
	if (outputFormat == 0) {
		seqsToPrint = (int *) malloc(numberOfSequences * sizeof(int));
		if(seqsToPrint == NULL){
			fprintf(stderr, "\nMemory Error\n%s\n", strerror(errno));
			fflush(stderr);
			exit(EXIT_FAILURE);
		}
		for (i = 0; i < numberOfSequences; i++) {
			seqsToPrint[i] = 0;
		}
	}
	// Look at the output of each thread.  Each file has output like
	// LINE1: 0 1 2 1 3 ....offsets
	// LINE2: support1 support2

	// Allocate some space to hold the lines
	myLine1 = (char *) malloc(MYBUFFER * sizeof(char));
	if(myLine1 == NULL){
		fprintf(stderr, "\nMemory Error\n%s\n", strerror(errno));
		fflush(stderr);
		exit(EXIT_FAILURE);
	}
	myLine2 = (char *) malloc(MYBUFFER * sizeof(char));
	if(myLine2 == NULL){
		fprintf(stderr, "\nMemory Error\n%s\n", strerror(errno));
		fflush(stderr);
		exit(EXIT_FAILURE);
	}
	lineSize = MYBUFFER;


	for (i = 0; i < numberOfThreads; i++) {
		rewind(OUTPUT[i]);

		// Check if the line is longer than the memory we've allocated
		if (lineSize < measureLine(OUTPUT[i])) {
			myLine1 = (char *) realloc(myLine1, measureLine(OUTPUT[i]) * sizeof(char));
			myLine2 = (char *) realloc(myLine2, measureLine(OUTPUT[i]) * sizeof(char));
			lineSize = measureLine(OUTPUT[i]);
		}
		while (fgets(myLine1, (int) lineSize + 1, OUTPUT[i]) != NULL) {
			if (outputFormat == 1) {
				// Check if the line is longer than the memory we've allocated
				if (lineSize < measureLine(OUTPUT[i])) {
					myLine1 = (char *) realloc(myLine1, measureLine(OUTPUT[i]) * sizeof(char));
					myLine2 = (char *) realloc(myLine2, measureLine(OUTPUT[i]) * sizeof(char));
					lineSize = measureLine(OUTPUT[i]);
				}
				fgets(myLine2, (int) lineSize + 1, OUTPUT[i]);
				myLine2[strlen(myLine2) - 1] = '\0';
				/*
				   fprintf(OUTPUT_FILE, "%s\t%s", myLine2, myLine1); 
				 */
				fprintf(OUTPUT_FILE, "%s\t", myLine2);
				fprintf(OUTPUT_FILE, "%s", myLine1);

			} else {
				sscanf(myLine1, "%d", &j);
				for (k = j - linesBefore; k <= j + linesAfter; k++) {
					seqsToPrint[k] = 1;
				}
			}
			// Check if the line is longer than the memory we've allocated
			if (lineSize < measureLine(OUTPUT[i])) {
				myLine1 = (char *) realloc(myLine1, measureLine(OUTPUT[i]) * sizeof(char));
				myLine2 = (char *) realloc(myLine2, measureLine(OUTPUT[i]) * sizeof(char));
				lineSize = measureLine(OUTPUT[i]);
			}
		}
	}
	if (outputFormat == 0) {
		for (i = 0; i < numberOfSequences; i++) {
			if (seqsToPrint[i] == 1) {
				if (printLabels == 1) {
					fprintf(OUTPUT_FILE, "%s\n", mySequences[i].label);
				}
				fprintf(OUTPUT_FILE, "%s\n", mySequences[i].seq);
			}
		}
		free(seqsToPrint);
	}
	free(myLine1);
	free(myLine2);
	return EXIT_SUCCESS;
}
const unsigned char *QgsDistanceArea::measurePolygon( const unsigned char* feature, double* area, double* perimeter, bool hasZptr )
{
  if ( !feature )
  {
    QgsDebugMsg( "no feature to measure" );
    return 0;
  }

  QgsConstWkbPtr wkbPtr( feature + 1 + sizeof( int ) );

  // get number of rings in the polygon
  int numRings;
  wkbPtr >> numRings;

  if ( numRings == 0 )
  {
    QgsDebugMsg( "no rings to measure" );
    return 0;
  }

  // Set pointer to the first ring
  QList<QgsPoint> points;
  QgsPoint pnt;
  double x, y;
  if ( area )
    *area = 0;
  if ( perimeter )
    *perimeter = 0;

  try
  {
    for ( int idx = 0; idx < numRings; idx++ )
    {
      int nPoints;
      wkbPtr >> nPoints;

      // Extract the points from the WKB and store in a pair of
      // vectors.
      for ( int jdx = 0; jdx < nPoints; jdx++ )
      {
        wkbPtr >> x >> y;
        if ( hasZptr )
        {
          // totally ignore Z value
          wkbPtr += sizeof( double );
        }

        pnt = QgsPoint( x, y );

        if ( mEllipsoidalMode && ( mEllipsoid != GEO_NONE ) )
        {
          pnt = mCoordTransform->transform( pnt );
        }
        points.append( pnt );
      }

      if ( points.size() > 2 )
      {
        if ( area )
        {
          double areaTmp = computePolygonArea( points );
          if ( idx == 0 )
          {
            // exterior ring
            *area += areaTmp;
          }
          else
          {
            *area -= areaTmp; // interior rings
          }
        }

        if ( perimeter )
        {
          if ( idx == 0 )
          {
            // exterior ring
            *perimeter += measureLine( points );
          }
        }
      }

      points.clear();
    }
  }
  catch ( QgsCsException &cse )
  {
    Q_UNUSED( cse );
    QgsMessageLog::logMessage( QObject::tr( "Caught a coordinate system exception while trying to transform a point. Unable to calculate polygon area or perimeter." ) );
  }

  return wkbPtr;
}
double QgsDistanceArea::measure( QgsGeometry* geometry )
{
  if ( !geometry )
    return 0.0;

  const unsigned char* wkb = geometry->asWkb();
  if ( !wkb )
    return 0.0;

  QgsConstWkbPtr wkbPtr( wkb + 1 );

  QGis::WkbType wkbType;
  wkbPtr >> wkbType;

  double res, resTotal = 0;
  int count, i;

  // measure distance or area based on what is the type of geometry
  bool hasZptr = false;

  switch ( wkbType )
  {
    case QGis::WKBLineString25D:
      hasZptr = true;
    case QGis::WKBLineString:
      measureLine( wkb, &res, hasZptr );
      QgsDebugMsg( "returning " + QString::number( res ) );
      return res;

    case QGis::WKBMultiLineString25D:
      hasZptr = true;
    case QGis::WKBMultiLineString:
      wkbPtr >> count;
      for ( i = 0; i < count; i++ )
      {
        wkbPtr = measureLine( wkbPtr, &res, hasZptr );
        resTotal += res;
      }
      QgsDebugMsg( "returning " + QString::number( resTotal ) );
      return resTotal;

    case QGis::WKBPolygon25D:
      hasZptr = true;
    case QGis::WKBPolygon:
      measurePolygon( wkb, &res, 0, hasZptr );
      QgsDebugMsg( "returning " + QString::number( res ) );
      return res;

    case QGis::WKBMultiPolygon25D:
      hasZptr = true;
    case QGis::WKBMultiPolygon:
      wkbPtr >> count;
      for ( i = 0; i < count; i++ )
      {
        wkbPtr = measurePolygon( wkbPtr, &res, 0, hasZptr );
        if ( !wkbPtr )
        {
          QgsDebugMsg( "measurePolygon returned 0" );
          break;
        }
        resTotal += res;
      }
      QgsDebugMsg( "returning " + QString::number( resTotal ) );
      return resTotal;

    default:
      QgsDebugMsg( QString( "measure: unexpected geometry type: %1" ).arg( wkbType ) );
      return 0;
  }
}