上海工程测量
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

600 lines
25 KiB

package com.project.survey.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;
import com.bingce.chart.ChartViewUtils;
import com.bingce.coordlib.model.Coordinate;
import com.bingce.data.surveyor.surveydata.pointsurvey.PointConstants;
import com.bingce.data.surveyor.surveydata.pointsurvey.PointRecord;
import com.bingce.poi.excel.ExcelReadHelper;
import com.bingce.utils.FileUtil;
import com.bingce.utils.PointUtils;
import com.bingce.utils.SoftKeyUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.project.survey.R;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import blankj.utilcode.util.ActivityUtils;
import blankj.utilcode.util.ToastUtils;
import blankj.utilcode.util.Utils;
import cn.liuyanbing.surveyor.model.base.BasePoint;
import cn.liuyanbing.surveyor.model.base.Road;
import cn.liuyanbing.surveyor.model.horizontalcurve.JDCurveElement;
import cn.liuyanbing.surveyor.model.horizontalcurve.JDElement;
import cn.liuyanbing.surveyor.model.horizontalcurve.JDFiveCurveElement;
import cn.liuyanbing.surveyor.model.horizontalcurve.XJDCurveElement;
import cn.liuyanbing.surveyor.model.horizontalcurve.XYCurveElement;
import lecho.hellocharts.model.Axis;
import lecho.hellocharts.model.Line;
import lecho.hellocharts.model.LineChartData;
import lecho.hellocharts.model.PointValue;
import lecho.hellocharts.view.LineChartView;
public class CommonUtils {
/**
* 隐藏软键盘
*/
public static void hideSoftInput() {
SoftKeyUtils.hideSoftKey(ActivityUtils.getTopActivity());
}
/**
* 创建文件在路径下
*
* @param path 路径{方法中包含sd卡路径}
* @param filename 文件名称
*/
public static File createMkdirsAndFiles(String path, String filename) {
if (TextUtils.isEmpty(path)) {
throw new RuntimeException("路径不能为空");
}
path = FileUtil.getSDPath() + File.separator + path;
File file = new File(path);
if (!file.exists()) {
try {
file.mkdirs();
} catch (Exception e) {
throw new RuntimeException("创建文件夹失败");
}
}
File file1 = new File(file, filename);
if (!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
throw new RuntimeException("创建文件失败");
}
}
return file1;
}
// /**
// * 移动文件
// *
// * @param oldFile 待移动文件
// * @param nDir 新目录
// * @param nName 新文件名称
// */
// public static boolean moveFileTo(File oldFile, String nDir, String nName) {
// if (oldFile == null) {
// ToastUtils.showShort("移动文件不存在-0");
// return false;
// }
// if (!oldFile.exists()) {
// ToastUtils.showShort("移动文件不存在-1");
// return false;
// }
// File fileDir = new File(FileUtil.getSDPath() + File.separator + nDir);
// if (!fileDir.exists()) {
// fileDir.mkdirs();
// }
// return oldFile.renameTo(new File(fileDir, nName));
// }
/**
* 将view转化为bitmap
*/
public static Bitmap view2Bitmap(View view) {
int w = view.getWidth();
int h = view.getHeight();
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(Color.WHITE);
view.layout(0, 0, w, h);
view.draw(canvas);
return bmp;
}
/**
* 将bitmap转化为数据流
*
* @return 生成的图片压缩20%
*/
public static InputStream bitmap2InputStream(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return bais;
}
/**
* 获取数据对象
*/
public static <T> List<T> mJson2Lists(String content, Class<T> cls) {
List<T> list = new ArrayList<>();
String json = content;
if (!TextUtils.isEmpty(json)) {
Gson gson = new Gson();
JsonArray array = new JsonParser().parse(json).getAsJsonArray();
for (JsonElement elem : array) {
list.add(gson.fromJson(elem, cls));
}
}
return list;
}
/**
* 数据转换为json格式
*/
public static <T> String mLists2Json(List<T> datalist) {
String result;
if (null == datalist || datalist.size() <= 0) {
result = "";
} else {
Gson gson = new Gson();
//转换成json数据,再保存
result = gson.toJson(datalist);
}
return result;
}
/**
* 设置chartview数据
*
* @param chart view
* @param datas 线
* @param hasAxis 是否设置边框
* @param isMirror 是否镜像
* @param referLineOffset 参考线的x坐标,例如设计线、隧道中线
*/
public static void mChartViewSetLineChartDatas(LineChartView chart, List<LineChartData> datas, boolean hasAxis, boolean isMirror, double... referLineOffset) {
ChartViewUtils.mChartViewSetLineChartDatas(chart, datas, hasAxis, isMirror, referLineOffset);
}
/**
* 设置chartview数据
*
* @param chart view
* @param datas 线
* @param hasAxis 是否设置边框
* @param isMirror 是否镜像
* @param referLineOffset 参考线的x坐标,例如设计线、隧道中线
*/
public static void mChartViewSetAndLableLineChartDatas(LineChartView chart, List<LineChartData> datas, boolean hasAxis, boolean isMirror, double... referLineOffset) {
if (datas != null) {
double left = Double.POSITIVE_INFINITY, top = Double.NEGATIVE_INFINITY, right = Double.NEGATIVE_INFINITY, bottom = Double.POSITIVE_INFINITY;
Rect contentRectMinusAll = chart.getChartComputator().getContentRectMinusAllMargins();
//总的线路集合
ArrayList<Line> lines = new ArrayList<>();
//判断数据是否为空
boolean hasLine = false;
//计算边界值和设计线
for (int i = 0; i < datas.size(); i++) {
for (Line line : datas.get(i).getLines()) {
// Calculate max and min for viewport.
for (PointValue pointValue : line.getValues()) {
if (pointValue.getX() < left) {
left = pointValue.getX();
}
if (pointValue.getX() > right) {
right = pointValue.getX();
}
if (pointValue.getY() < bottom) {
bottom = pointValue.getY();
}
if (pointValue.getY() > top) {
top = pointValue.getY();
}
}
hasLine = true;
}
}
if (hasLine && referLineOffset.length != 0) {
//计算边界时参考线也要考虑在内
for (int i = 0; i < referLineOffset.length; i++) {
if (referLineOffset[i] < left) {
left = referLineOffset[i];
}
if (referLineOffset[i] > right) {
right = referLineOffset[i];
}
}
double width = right - left, height = top - bottom;
if (contentRectMinusAll.height() / height >= contentRectMinusAll.width() / width) {
// Y轴增量
double deltaY = width / contentRectMinusAll.width() * contentRectMinusAll.height() - height;
//Log.i(TAG, "Y增量:" + deltaY);
//后面/30是在编辑加入缓存区,以免图形显示不全
for (int i = 0; i < referLineOffset.length; i++) {
List<PointValue> lineValues = new ArrayList<>();
lineValues.add(new PointValue(referLineOffset[i], top + deltaY / 2));
lineValues.add(new PointValue(referLineOffset[i], bottom - deltaY / 2));
Line line = new Line(lineValues).setPointColor(Color.MAGENTA).setHasLabels(true).setStrokeWidth(1).setPointRadius(2);
line.setPathEffect(new DashPathEffect(new float[]{2, 4, 8, 16}, 1));
lines.add(line);
}
} else {
for (int i = 0; i < referLineOffset.length; i++) {
List<PointValue> lineValues = new ArrayList<>();
lineValues.add(new PointValue(referLineOffset[i], top));
lineValues.add(new PointValue(referLineOffset[i], bottom));
Line line = new Line(lineValues).setPointColor(Color.MAGENTA).setHasLabels(true).setStrokeWidth(1).setPointRadius(2);
line.setPathEffect(new DashPathEffect(new float[]{2, 4, 8, 16}, 1));
lines.add(line);
}
}
}
//把参数datas集成
for (int i = 0; i < datas.size(); i++) {
lines.addAll(datas.get(i).getLines());
}
//镜像
if (isMirror) {
for (Line line : lines) {
for (PointValue pointValue : line.getValues()) {
pointValue.set(-1 * pointValue.getX(), pointValue.getY());
}
}
}
LineChartData resultData = new LineChartData(lines);
if (hasAxis) {
resultData.setAxisYLeft(new Axis().setHasLines(false).setHasSeparationLine(true));
resultData.setAxisYRight(new Axis().setHasLines(false).setHasSeparationLine(true));
resultData.setAxisXBottom(new Axis().setHasLines(false).setHasSeparationLine(true));
resultData.setAxisXTop(new Axis().setHasLines(false).setHasSeparationLine(true));
} else {
resultData.setAxisYLeft(null);
resultData.setAxisYRight(null);
resultData.setAxisXBottom(null);
resultData.setAxisXTop(null);
}
resultData.setValueLabelBackgroundEnabled(true);//隐藏label背景,放到这里设置是因为这是最后一关,不会被其他data的背景色覆盖
resultData.setValueLabelsTextColor(Color.WHITE);
chart.setLineChartData(resultData);
chart.setValueSelectionEnabled(true);
} else {
chart.setLineChartData(null);
}
}
/**
* 导入exl文件
*
* @param remarks 备注
* @param file 文件路径
*/
public static List<PointRecord> importExl(Context context, String projectId, String jobId, String roadId, String remarks, File file) {
long time = new Date().getTime();
List<PointRecord> records = new ArrayList<>();
int line = 0;
try {
List<List<String>> iResult = ExcelReadHelper.excelRead(file, ExcelReadHelper.FIRST_ROW_NOT_IGNORE);
if (iResult == null || iResult.isEmpty()) {
return null;
}
for (int i = 0; i < iResult.size(); i++) {
List<String> row = iResult.get(i);
line = i + 1;
if (row.isEmpty() || ExcelReadHelper.isRowEmpty(row) || row.size() < 3)
continue;
if (row.size() == 3) {
PointRecord record = new PointRecord(projectId, jobId, roadId,
PointConstants.POINT_TYPE_SURVEY_AREA, line + "", line + ""
, remarks == null ? context.getString(R.string.record_none) : remarks
, Double.parseDouble(row.get(0)), Double.parseDouble(row.get(1)), Double.parseDouble(row.get(2)), 0, 0, 0
, PointConstants.POINT_FORMAT_XYZ, null);
record.createDate = new Date(time + i);
records.add(record);
}
if (row.size() == 4) {
PointRecord record = new PointRecord(projectId, jobId, roadId,
PointConstants.POINT_TYPE_SURVEY_AREA, row.get(0), row.get(0)
, remarks == null ? context.getString(R.string.record_none) : remarks
, Double.parseDouble(row.get(1)), Double.parseDouble(row.get(2)), Double.parseDouble(row.get(3)), 0, 0, 0
, PointConstants.POINT_FORMAT_XYZ, null);
record.createDate = new Date(time + i);
records.add(record);
}
}
ToastUtils.showLong(file.getAbsolutePath() + ",导入成功");
return records;
} catch (Exception e) {
ToastUtils.showLong(file.getAbsolutePath() + ",导入失败.Error:第" + line + "行,数据格式有误.可能存在非数组坐标");
e.printStackTrace();
return null;
}
}
/**
* 导入txt文件
*
* @param context 上下文
* @param jobId 作业Id
* @param remarks 备注
* @param file 文件
*/
public static List<PointRecord> importTxt(Context context, String projectId, String jobId, String roadId, String remarks, File file) {
long time = new Date().getTime();
List<PointRecord> records = new ArrayList<>();
String result = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
BufferedReader buff = null;
int line = 0;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
buff = new BufferedReader(new InputStreamReader(bis, "utf-8"));
String a = null;
while ((a = buff.readLine()) != null)// 检查数据
{
line++;
if ("".equals(a.trim())) {
continue;
}
a = a.replaceAll("(^[\u0020\u0009\u3000]*)|([\u0020\u0009\u3000]*$)", "");// 删除首尾空格、制表及全角空格
String[] arr = a.split(",");
if (arr.length < 3) {
result = file.getAbsolutePath() + ",导入失败,原因:第" + line + "行,元素个数不能少于4个";
continue;
}
if (arr.length == 3) {
PointRecord record = new PointRecord(projectId, jobId, roadId,
PointConstants.POINT_TYPE_SURVEY_AREA, line + "", line + ""
, remarks == null ? context.getString(R.string.record_none) : remarks
, Double.parseDouble(arr[0]), Double.parseDouble(arr[1]), Double.parseDouble(arr[2]), 0, 0, 0
, PointConstants.POINT_FORMAT_XYZ, null);
record.createDate = new Date(time++);
records.add(record);
}
if (arr.length == 4) {
PointRecord record = new PointRecord(projectId, jobId, roadId,
PointConstants.POINT_TYPE_SURVEY_AREA, arr[0], arr[0]
, remarks == null ? context.getString(R.string.record_none) : remarks
, Double.parseDouble(arr[1]), Double.parseDouble(arr[2]), Double.parseDouble(arr[3]), 0, 0, 0
, PointConstants.POINT_FORMAT_XYZ, null);
record.createDate = new Date(time++);
records.add(record);
}
}
if (result != null)
ToastUtils.showLong(result);
return records;
} catch (NumberFormatException e) {
result = file.getAbsolutePath() + ",导入失败,原因:第" + line + "行,发现非数字字符串";
ToastUtils.showLong(result);
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (buff != null) {
try {
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static LineChartData mCircleChartDatas(int color, double centerX, double centerY, double radius, double anglePlus) {
Line circleLine = new Line(CalCirclePathPoints(centerX, centerY, radius, anglePlus));
circleLine.setStrokeWidth(1);
circleLine.setHasPoints(false);
circleLine.setCubic(true);
circleLine.setColor(color);
circleLine.setHasLines(true);
circleLine.setHasLabels(false);
LineChartData data = new LineChartData();
data.getLines().add(circleLine);
return data;
}
private static ArrayList<PointValue> CalCirclePathPoints(double centerX, double centerY, double radius, double anglePlus) {
ArrayList<PointValue> result = new ArrayList();
result.add(new PointValue(centerY, centerX + radius));
double center2BeginAngle = 0.0D;
double angle = Math.toRadians(Math.abs(anglePlus));
for (int i = 1; angle * (double) i < 6.283185307179586D; ++i) {
double x = centerX + radius * Math.cos(center2BeginAngle + angle * (double) i);
double y = centerY + radius * Math.sin(center2BeginAngle + angle * (double) i);
PointValue pv = new PointValue(((float) y), ((float) x));
result.add(pv);
}
result.add(new PointValue(centerY, centerX + radius));
return result;
}
/**
* huo
*
* @param startK
* @param endK
* @return
*/
public static List<Double> getMainPoints(Road currentRoad, double startK, double endK) {
List<Double> mainPoints = new ArrayList<>();
if (currentRoad.isUseJD()) {
for (int i = 0; i < currentRoad.getJdHorList().size(); i++) {
JDElement jde = currentRoad.getJdHorList().get(i);
if (jde.isCurveElement()) {
JDCurveElement curveElement = JDCurveElement.dynamic_cast(jde);
if (curveElement.getZHK() >= startK && curveElement.getZHK() <= endK) {
mainPoints.add(curveElement.getZHK());
}
if (curveElement.getHYK() >= startK && curveElement.getHYK() <= endK) {
mainPoints.add(curveElement.getHYK());
}
if (curveElement.getQZK() >= startK && curveElement.getQZK() <= endK) {
mainPoints.add(curveElement.getQZK());
}
if (curveElement.getYHK() >= startK && curveElement.getYHK() <= endK) {
mainPoints.add(curveElement.getYHK());
}
if (curveElement.getHZK() >= startK && curveElement.getHZK() <= endK) {
mainPoints.add(curveElement.getHZK());
}
} else if (jde.isFiveCurveElement()) {
JDFiveCurveElement fiveCurveElement = JDFiveCurveElement.dynamic_cast(jde);
if (fiveCurveElement.getZHK() >= startK && fiveCurveElement.getZHK() <= endK) {
mainPoints.add(fiveCurveElement.getZHK());
}
if (fiveCurveElement.getHYK1() >= startK && fiveCurveElement.getHYK1() <= endK) {
mainPoints.add(fiveCurveElement.getHYK1());
}
if (fiveCurveElement.getQZK1() >= startK && fiveCurveElement.getQZK1() <= endK) {
mainPoints.add(fiveCurveElement.getQZK1());
}
if (fiveCurveElement.getYHK1() >= startK && fiveCurveElement.getYHK1() <= endK) {
mainPoints.add(fiveCurveElement.getYHK1());
}
if (fiveCurveElement.getHYK2() >= startK && fiveCurveElement.getHYK2() <= endK) {
mainPoints.add(fiveCurveElement.getHYK2());
}
if (fiveCurveElement.getQZK2() >= startK && fiveCurveElement.getQZK2() <= endK) {
mainPoints.add(fiveCurveElement.getQZK2());
}
if (fiveCurveElement.getYHK2() >= startK && fiveCurveElement.getYHK2() <= endK) {
mainPoints.add(fiveCurveElement.getYHK2());
}
if (fiveCurveElement.getHZK() >= startK && fiveCurveElement.getHZK() <= endK) {
mainPoints.add(fiveCurveElement.getHZK());
}
} else if (jde.isXJDCurveElement()) {
XJDCurveElement xjd = XJDCurveElement.dynamic_cast(jde);
if (xjd.getZHK() >= startK && xjd.getZHK() <= endK) {
mainPoints.add(xjd.getZHK());
}
if (xjd.getHYK() >= startK && xjd.getHYK() <= endK) {
mainPoints.add(xjd.getHYK());
}
if (xjd.getQZK() >= startK && xjd.getQZK() <= endK) {
mainPoints.add(xjd.getQZK());
}
if (xjd.getYHK() >= startK && xjd.getYHK() <= endK) {
mainPoints.add(xjd.getYHK());
}
if (xjd.getHZK() >= startK && xjd.getHZK() <= endK) {
mainPoints.add(xjd.getHZK());
}
}
}
} else {
for (int i = 0; i < currentRoad.getHorList().size(); i++) {
XYCurveElement ce = currentRoad.getHorList().get(i);
if (ce.isLineElement()) {
if (ce.getStartPoint().getK() >= startK && ce.getStartPoint().getK() <= endK) {
mainPoints.add(ce.getStartPoint().getK());
}
} else if (ce.isInTransCurveElement()) {
if (ce.getStartPoint().getK() >= startK && ce.getStartPoint().getK() <= endK) {
mainPoints.add(ce.getStartPoint().getK());
}
} else if (ce.isCircleCurveElement()) {
if (ce.getStartPoint().getK() >= startK && ce.getStartPoint().getK() <= endK) {
mainPoints.add(ce.getStartPoint().getK());
mainPoints.add((ce.getStartPoint().getK() + ce.getEndPoint().getK()) / 2);
}
} else if (ce.isOutTransCurveElement()) {
if (ce.getStartPoint().getK() >= startK && ce.getStartPoint().getK() <= endK) {
mainPoints.add(ce.getStartPoint().getK());
}
} else if (ce.isOvateCurveElement()) {
if (ce.getStartPoint().getK() >= startK && ce.getStartPoint().getK() <= endK) {
mainPoints.add(ce.getStartPoint().getK());
}
}
}
}
return mainPoints;
}
// public static McGePoint3d wcsToUcs(McGePoint3d point3d) {
// return McGePointUtils.wcsToUcs(point3d);
// }
//
// public static McGePoint3d ucsToWcs(McGePoint3d point3d) {
// return McGePointUtils.ucsToWcs(point3d);
// }
public static BasePoint toBasePoint(Coordinate coordinate) {
return PointUtils.toBasePoint(coordinate);
}
/**
* dp 转成 px
*
* @param dpValue
* @return
*/
public static int dip2px(float dpValue) {
final float scale = (Utils.getApp()).getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 判断导出文件是否存在
*
* @param filePaths 文件路径
*/
public static boolean isCheckExistFile(String filePaths) {
File file = new File(filePaths);
return file.exists();
}
}