<commit_before>/*
 Copyright 1995-2015 Esri

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts Dept
 380 New York Street
 Redlands, California, USA 92373

 email: <EMAIL>
 */

package com.esri.core.geometry;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

class OperatorExportToWkbLocal extends OperatorExportToWkb {

	@Override
	public ByteBuffer execute(int exportFlags, Geometry geometry,
			ProgressTracker progressTracker) {
		int size = exportToWKB(exportFlags, geometry, null);
		ByteBuffer wkbBuffer = ByteBuffer.allocate(size).order(
				ByteOrder.nativeOrder());
		exportToWKB(exportFlags, geometry, wkbBuffer);
		return wkbBuffer;
	}

	@Override
	public int execute(int exportFlags, Geometry geometry,
			ByteBuffer wkbBuffer, ProgressTracker progressTracker) {
		return exportToWKB(exportFlags, geometry, wkbBuffer);
	}

	private static int exportToWKB(int exportFlags, Geometry geometry,
			ByteBuffer wkbBuffer) {
		if (geometry == null)
			return 0;

		int type = geometry.getType().value();
		switch (type) {
		case Geometry.GeometryType.Polygon:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPoint) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPoint) != 0)
				throw new GeometryException("invalid argument");

			return exportPolygonToWKB(exportFlags, (Polygon) geometry,
					wkbBuffer);
		case Geometry.GeometryType.Polyline:
			if ((exportFlags & WkbExportFlags.wkbExportPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPoint) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPoint) != 0)
				throw new GeometryException("invalid argument");
			return exportPolylineToWKB(exportFlags, (Polyline) geometry,
					wkbBuffer);

		case Geometry.GeometryType.MultiPoint:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPolygon) != 0)
				throw new GeometryException("invalid argument");
			return exportMultiPointToWKB(exportFlags, (MultiPoint) geometry,
					wkbBuffer);

		case Geometry.GeometryType.Point:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPolygon) != 0)
				throw new GeometryException("invalid argument");
			return exportPointToWKB(exportFlags, (Point) geometry, wkbBuffer);

		case Geometry.GeometryType.Envelope:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPoint) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPoint) != 0)
				throw new GeometryException("invalid argument");
			return exportEnvelopeToWKB(exportFlags, (Envelope) geometry,
					wkbBuffer);

		default: {
			throw GeometryException.GeometryInternalError();
			// return -1;
		}
		}
	}

	private static int exportPolygonToWKB(int exportFlags, Polygon _polygon,
			ByteBuffer wkbBuffer) {
		MultiPathImpl polygon = (MultiPathImpl) _polygon._getImpl();

		if ((exportFlags & (int) WkbExportFlags.wkbExportFailIfNotSimple) != 0) {
			int simple = polygon.getIsSimple(0.0);

			if (simple != MultiVertexGeometryImpl.GeometryXSimple.Strong)
				throw new GeometryException("non simple geometry");
		}

		boolean bExportZs = polygon.hasAttribute(VertexDescription.Semantics.Z)
				&& (exportFlags & (int) WkbExportFlags.wkbExportStripZs) == 0;
		boolean bExportMs = polygon.hasAttribute(VertexDescription.Semantics.M)
				&& (exportFlags & (int) WkbExportFlags.wkbExportStripMs) == 0;

		int polygonCount = polygon.getOGCPolygonCount();
		if ((exportFlags & (int) WkbExportFlags.wkbExportPolygon) != 0
				&& polygonCount > 1)
			throw new IllegalArgumentException();

		int partCount = polygon.getPathCount();
		int point_count = polygon.getPointCount();
		point_count += partCount; // add 1 point per part

		if (point_count > 0 && polygonCount == 0)
			throw new GeometryException("corrupted geometry");

		// In the WKB_export_defaults case, polygons gets exported as a
		// WKB_multi_polygon.

		// get size for buffer
		int size = 0;
		if ((exportFlags & (int) WkbExportFlags.wkbExportPolygon) == 0
				|| polygonCount == 0)
			size += 1 /* byte order */+ 4 /* wkbType */+ 4 /* numPolygons */;

		size += polygonCount
				* (1 /* byte order */+ 4 /* wkbType */+ 4/* numRings */)
				+ partCount * (4 /* num_points */) + point_count * (2 * 8 /*
																		 * xy
																		 * coordinates
																		 */);

		if (bExportZs)
			size += (point_count * 8 /* zs */);
		if (bExportMs)
			size += (point_count * 8 /* ms */);

		if (size >= NumberUtils.intMax())
			throw new GeometryException("invalid call");

		if (wkbBuffer == null)
			return (int) size;
		else if (wkbBuffer.capacity() < size)
			throw new GeometryException("buffer is too small");

		int offset = 0;

		byte byteOrder = (byte) (wkbBuffer.order() == ByteOrder.LITTLE_ENDIAN ? WkbByteOrder.wkbNDR
				: WkbByteOrder.wkbXDR);

		// Determine the wkb type
		int type;
		if (!bExportZs && !bExportMs) {
			type = WkbGeometryType.wkbPolygon;

			if ((exportFlags & WktExportFlags.wktExportPolygon) == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
				wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiPolygon);
				offset += 4;
				wkbBuffer.putInt(offset, polygonCount);
				offset += 4;
			} else if (polygonCount == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
				wkbBuffer.putInt(offset, WkbGeometryType.wkbPolygon);
				offset += 4;
				wkbBuffer.putInt(offset, 0);
				offset += 4;
			}
		} else if (bExportZs && !bExportMs) {
			type = WkbGeometryType.wkbPolygonZ;

			if ((exportFlags & WkbExportFlags.wkbExportPolygon) == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
				wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiPolygonZ);
				offset += 4;
				wkbBuffer.putInt(offset, polygonCount);
				offset += 4;
			} else if (polygonCount == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;<commit_msg>Fix a typo in usage of Export flags (#171)<commit_after>/*
 Copyright 1995-2015 Esri

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts Dept
 380 New York Street
 Redlands, California, USA 92373

 email: <EMAIL>
 */

package com.esri.core.geometry;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

class OperatorExportToWkbLocal extends OperatorExportToWkb {

	@Override
	public ByteBuffer execute(int exportFlags, Geometry geometry,
			ProgressTracker progressTracker) {
		int size = exportToWKB(exportFlags, geometry, null);
		ByteBuffer wkbBuffer = ByteBuffer.allocate(size).order(
				ByteOrder.nativeOrder());
		exportToWKB(exportFlags, geometry, wkbBuffer);
		return wkbBuffer;
	}

	@Override
	public int execute(int exportFlags, Geometry geometry,
			ByteBuffer wkbBuffer, ProgressTracker progressTracker) {
		return exportToWKB(exportFlags, geometry, wkbBuffer);
	}

	private static int exportToWKB(int exportFlags, Geometry geometry,
			ByteBuffer wkbBuffer) {
		if (geometry == null)
			return 0;

		int type = geometry.getType().value();
		switch (type) {
		case Geometry.GeometryType.Polygon:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPoint) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPoint) != 0)
				throw new GeometryException("invalid argument");

			return exportPolygonToWKB(exportFlags, (Polygon) geometry,
					wkbBuffer);
		case Geometry.GeometryType.Polyline:
			if ((exportFlags & WkbExportFlags.wkbExportPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPoint) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPoint) != 0)
				throw new GeometryException("invalid argument");
			return exportPolylineToWKB(exportFlags, (Polyline) geometry,
					wkbBuffer);

		case Geometry.GeometryType.MultiPoint:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPolygon) != 0)
				throw new GeometryException("invalid argument");
			return exportMultiPointToWKB(exportFlags, (MultiPoint) geometry,
					wkbBuffer);

		case Geometry.GeometryType.Point:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPolygon) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPolygon) != 0)
				throw new GeometryException("invalid argument");
			return exportPointToWKB(exportFlags, (Point) geometry, wkbBuffer);

		case Geometry.GeometryType.Envelope:
			if ((exportFlags & WkbExportFlags.wkbExportLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiLineString) != 0
					|| (exportFlags & WkbExportFlags.wkbExportPoint) != 0
					|| (exportFlags & WkbExportFlags.wkbExportMultiPoint) != 0)
				throw new GeometryException("invalid argument");
			return exportEnvelopeToWKB(exportFlags, (Envelope) geometry,
					wkbBuffer);

		default: {
			throw GeometryException.GeometryInternalError();
			// return -1;
		}
		}
	}

	private static int exportPolygonToWKB(int exportFlags, Polygon _polygon,
			ByteBuffer wkbBuffer) {
		MultiPathImpl polygon = (MultiPathImpl) _polygon._getImpl();

		if ((exportFlags & (int) WkbExportFlags.wkbExportFailIfNotSimple) != 0) {
			int simple = polygon.getIsSimple(0.0);

			if (simple != MultiVertexGeometryImpl.GeometryXSimple.Strong)
				throw new GeometryException("non simple geometry");
		}

		boolean bExportZs = polygon.hasAttribute(VertexDescription.Semantics.Z)
				&& (exportFlags & (int) WkbExportFlags.wkbExportStripZs) == 0;
		boolean bExportMs = polygon.hasAttribute(VertexDescription.Semantics.M)
				&& (exportFlags & (int) WkbExportFlags.wkbExportStripMs) == 0;

		int polygonCount = polygon.getOGCPolygonCount();
		if ((exportFlags & (int) WkbExportFlags.wkbExportPolygon) != 0
				&& polygonCount > 1)
			throw new IllegalArgumentException();

		int partCount = polygon.getPathCount();
		int point_count = polygon.getPointCount();
		point_count += partCount; // add 1 point per part

		if (point_count > 0 && polygonCount == 0)
			throw new GeometryException("corrupted geometry");

		// In the WKB_export_defaults case, polygons gets exported as a
		// WKB_multi_polygon.

		// get size for buffer
		int size = 0;
		if ((exportFlags & (int) WkbExportFlags.wkbExportPolygon) == 0
				|| polygonCount == 0)
			size += 1 /* byte order */+ 4 /* wkbType */+ 4 /* numPolygons */;

		size += polygonCount
				* (1 /* byte order */+ 4 /* wkbType */+ 4/* numRings */)
				+ partCount * (4 /* num_points */) + point_count * (2 * 8 /*
																		 * xy
																		 * coordinates
																		 */);

		if (bExportZs)
			size += (point_count * 8 /* zs */);
		if (bExportMs)
			size += (point_count * 8 /* ms */);

		if (size >= NumberUtils.intMax())
			throw new GeometryException("invalid call");

		if (wkbBuffer == null)
			return (int) size;
		else if (wkbBuffer.capacity() < size)
			throw new GeometryException("buffer is too small");

		int offset = 0;

		byte byteOrder = (byte) (wkbBuffer.order() == ByteOrder.LITTLE_ENDIAN ? WkbByteOrder.wkbNDR
				: WkbByteOrder.wkbXDR);

		// Determine the wkb type
		int type;
		if (!bExportZs && !bExportMs) {
			type = WkbGeometryType.wkbPolygon;

			if ((exportFlags & WkbExportFlags.wkbExportPolygon) == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
				wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiPolygon);
				offset += 4;
				wkbBuffer.putInt(offset, polygonCount);
				offset += 4;
			} else if (polygonCount == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
				wkbBuffer.putInt(offset, WkbGeometryType.wkbPolygon);
				offset += 4;
				wkbBuffer.putInt(offset, 0);
				offset += 4;
			}
		} else if (bExportZs && !bExportMs) {
			type = WkbGeometryType.wkbPolygonZ;

			if ((exportFlags & WkbExportFlags.wkbExportPolygon) == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
				wkbBuffer.putInt(offset, WkbGeometryType.wkbMultiPolygonZ);
				offset += 4;
				wkbBuffer.putInt(offset, polygonCount);
				offset += 4;
			} else if (polygonCount == 0) {
				wkbBuffer.put(offset, byteOrder);
				offset += 1;
{'fraction_non_alphanumeric': 0.11052743664371406, 'fraction_numerical': 0.011854901682402216, 'mean_word_length': 3.958817317845829, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '258183', 'n_tokens_mistral': 5280, 'n_tokens_neox': 4295, 'n_words': 1089}